diff options
author | Roman Birg <roman@cyngn.com> | 2015-01-19 16:22:56 -0800 |
---|---|---|
committer | Danesh M <daneshm90@gmail.com> | 2015-12-09 10:42:51 -0800 |
commit | 3187e88f31c8bd5a4cf2b2b45f9cda5a77dc88d8 (patch) | |
tree | 1985083793a370dccb57a2ee8c20647f4f951690 /packages | |
parent | 6be220df8c0368c58b9ac2a01254844263bac925 (diff) | |
download | frameworks_base-3187e88f31c8bd5a4cf2b2b45f9cda5a77dc88d8.zip frameworks_base-3187e88f31c8bd5a4cf2b2b45f9cda5a77dc88d8.tar.gz frameworks_base-3187e88f31c8bd5a4cf2b2b45f9cda5a77dc88d8.tar.bz2 |
SystemUI: port compass tile to cm-13.0
Change-Id: I4cce9c849a702947c0cf00e3e1293f89c985b9e9
Diffstat (limited to 'packages')
6 files changed, 275 insertions, 0 deletions
diff --git a/packages/SystemUI/res/drawable/ic_qs_compass_off.xml b/packages/SystemUI/res/drawable/ic_qs_compass_off.xml new file mode 100644 index 0000000..e1ebfec --- /dev/null +++ b/packages/SystemUI/res/drawable/ic_qs_compass_off.xml @@ -0,0 +1,32 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* + * Copyright (C) 2015 The CyanogenMod Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +--> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="64dp" + android:height="64dp" + android:viewportWidth="48" + android:viewportHeight="48"> + + <path + android:fillColor="#4DFFFFFF" + android:pathData="M42,39.2L8.8,6L6,8.8l9.8,9.8l-1.5,3.7c-0.4,1-0.4,2.2,0,3.3l7.8,19.1c0.7,1.7,3.1,1.7,3.8,0l4.6-11.3 +l8.6,8.6L42,39.2Z +M24,28c-2.2,0-4-1.8-4-4c0-0.4,0.1-0.7,0.1-1l4.9,4.9C24.7,27.9,24.4,28,24,28z +M33.8,25.3L19.1,10.7l3-7.4 +c0.7-1.7,3.1-1.7,3.8,0l7.8,19.1C34.1,23.3,34.1,24.4,33.8,25.3z" /> +</vector>
\ No newline at end of file diff --git a/packages/SystemUI/res/drawable/ic_qs_compass_on.xml b/packages/SystemUI/res/drawable/ic_qs_compass_on.xml new file mode 100644 index 0000000..f66f64c --- /dev/null +++ b/packages/SystemUI/res/drawable/ic_qs_compass_on.xml @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="utf-8"?> +<!-- +/* + * Copyright (C) 2015 The CyanogenMod Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +--> +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="48dp" + android:height="48dp" + android:viewportWidth="48" + android:viewportHeight="48"> + + <path + android:fillColor="#FFFFFF" + android:pathData="M25.9,3.3l7.8,19.1c0.4,1,0.4,2.2,0,3.3l-7.8,19.1c-0.7,1.7-3.1,1.7-3.8,0l-7.8-19.1c-0.4-1-0.4-2.2,0-3.3 +l7.8-19.1C22.8,1.6,25.2,1.6,25.9,3.3Z +M24,20c-2.2,0-4,1.8-4,4s1.8,4,4,4s4-1.8,4-4S26.2,20,24,20z +M26,14v-4c0-1.1-0.9-2-2-2 s-2,0.9-2,2v4c0,1.1,0.9,2,2,2S26,15.1,26,14z" /> +</vector>
\ No newline at end of file diff --git a/packages/SystemUI/res/values/cm_arrays.xml b/packages/SystemUI/res/values/cm_arrays.xml index a9e3654..cabf612 100644 --- a/packages/SystemUI/res/values/cm_arrays.xml +++ b/packages/SystemUI/res/values/cm_arrays.xml @@ -24,4 +24,16 @@ <item>369</item><item>157</item> <item>369</item><item>0</item> </array> + + <!-- Cardinal directions for the compass tile --> + <string-array name="cardinal_directions"> + <item>N</item> <!-- North --> + <item>NE</item> <!-- North east --> + <item>E</item> <!-- East --> + <item>SE</item> <!-- South east --> + <item>S</item> <!-- South --> + <item>SW</item> <!-- South west --> + <item>W</item> <!-- West --> + <item>NW</item> <!-- North west --> + </string-array> </resources> diff --git a/packages/SystemUI/res/values/cm_strings.xml b/packages/SystemUI/res/values/cm_strings.xml index 69c8ee0..964b217 100644 --- a/packages/SystemUI/res/values/cm_strings.xml +++ b/packages/SystemUI/res/values/cm_strings.xml @@ -68,6 +68,9 @@ <!-- Content description of the light brightness slider (not shown on the screen). [CHAR LIMIT=NONE] --> <string name="accessibility_notification_brightness">Light brightness</string> + <string name="quick_settings_compass_value" translatable="false"><xliff:g id="degrees">%1$.0f</xliff:g>\u00b0 <xliff:g id="direction">%2$s</xliff:g></string> + <string name="quick_settings_compass_off">Compass off</string> + <string name="quick_settings_compass_init">Initializing\u2026</string> <!-- Lights settings, LED notification --> <string name="led_notification_title">Light settings</string> @@ -91,4 +94,5 @@ <string name="quick_settings_network_adb_enabled_label">Enabled</string> <string name="quick_settings_network_adb_disabled_label">Disabled</string> <string name="qs_tile_adb_over_network">ADB over network</string> + <string name="qs_tile_compass">Compass</string> </resources> diff --git a/packages/SystemUI/src/com/android/systemui/qs/tiles/CompassTile.java b/packages/SystemUI/src/com/android/systemui/qs/tiles/CompassTile.java new file mode 100644 index 0000000..f7af7f9 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/qs/tiles/CompassTile.java @@ -0,0 +1,193 @@ +/* + * Copyright (C) 2015 The CyanogenMod Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.qs.tiles; + +import android.content.Context; +import android.hardware.Sensor; +import android.hardware.SensorEvent; +import android.hardware.SensorEventListener; +import android.hardware.SensorManager; +import android.widget.ImageView; + +import com.android.internal.logging.MetricsConstants; +import com.android.systemui.R; +import com.android.systemui.qs.QSTile; +import com.android.systemui.qs.QSTileView; + +public class CompassTile extends QSTile<QSTile.BooleanState> implements SensorEventListener { + private final static float ALPHA = 0.97f; + + private boolean mActive = false; + + private SensorManager mSensorManager; + private Sensor mAccelerationSensor; + private Sensor mGeomagneticFieldSensor; + + private float[] mAcceleration; + private float[] mGeomagnetic; + + private ImageView mImage; + private boolean mListeningSensors; + + public CompassTile(Host host) { + super(host); + mSensorManager = (SensorManager) mContext.getSystemService(Context.SENSOR_SERVICE); + mAccelerationSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); + mGeomagneticFieldSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD); + } + + @Override + protected BooleanState newTileState() { + return new BooleanState(); + } + + @Override + protected void handleDestroy() { + super.handleDestroy(); + setListeningSensors(false); + mSensorManager = null; + mImage = null; + } + + @Override + public QSTileView createTileView(Context context) { + QSTileView tileView = super.createTileView(context); + mImage = (ImageView) tileView.findViewById(android.R.id.icon); + return tileView; + } + + @Override + protected void handleClick() { + mActive = !mActive; + refreshState(); + setListeningSensors(mActive); + } + + private void setListeningSensors(boolean listening) { + if (listening == mListeningSensors) return; + mListeningSensors = listening; + if (mListeningSensors) { + mSensorManager.registerListener( + this, mAccelerationSensor, SensorManager.SENSOR_DELAY_GAME); + mSensorManager.registerListener( + this, mGeomagneticFieldSensor, SensorManager.SENSOR_DELAY_GAME); + } else { + mSensorManager.unregisterListener(this); + } + } + + @Override + protected void handleUpdateState(BooleanState state, Object arg) { + Float degrees = arg == null ? 0 :(float) arg; + + state.visible = true; + state.value = mActive; + + if (state.value) { + state.icon = ResourceIcon.get(R.drawable.ic_qs_compass_on); + if (arg != null) { + state.label = formatValueWithCardinalDirection(degrees); + + float target = 360 - degrees; + float relative = target - mImage.getRotation(); + if (relative > 180) relative -= 360; + + mImage.setRotation(mImage.getRotation() + relative / 2); + + } else { + state.label = mContext.getString(R.string.quick_settings_compass_init); + mImage.setRotation(0); + } + } else { + state.icon = ResourceIcon.get(R.drawable.ic_qs_compass_off); + state.label = mContext.getString(R.string.quick_settings_compass_off); + mImage.setRotation(0); + } + } + + @Override + public int getMetricsCategory() { + return MetricsConstants.DONT_TRACK_ME_BRO; + } + + @Override + public void setListening(boolean listening) { + if (!listening) { + setListeningSensors(false); + mActive = false; + } + } + + private String formatValueWithCardinalDirection(float degree) { + int cardinalDirectionIndex = (int) (Math.floor(((degree - 22.5) % 360) / 45) + 1) % 8; + String[] cardinalDirections = mContext.getResources().getStringArray( + R.array.cardinal_directions); + + return mContext.getString(R.string.quick_settings_compass_value, degree, + cardinalDirections[cardinalDirectionIndex]); + } + + @Override + public void onSensorChanged(SensorEvent event) { + float[] values; + if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) { + if (mAcceleration == null) { + mAcceleration = event.values.clone(); + } + + values = mAcceleration; + } else { + // Magnetic field sensor + if (mGeomagnetic == null) { + mGeomagnetic = event.values.clone(); + } + + values = mGeomagnetic; + } + + for (int i = 0; i < 3; i++) { + values[i] = ALPHA * values[i] + (1 - ALPHA) * event.values[i]; + } + + if (!mActive || !mListeningSensors || mAcceleration == null || mGeomagnetic == null) { + // Nothing to do at this moment + return; + } + + float R[] = new float[9]; + float I[] = new float[9]; + if (!SensorManager.getRotationMatrix(R, I, mAcceleration, mGeomagnetic)) { + // Rotation matrix couldn't be calculated + return; + } + + // Get the current orientation + float[] orientation = new float[3]; + SensorManager.getOrientation(R, orientation); + + // Convert azimuth to degrees + Float newDegree = Float.valueOf((float) Math.toDegrees(orientation[0])); + newDegree = (newDegree + 360) % 360; + + refreshState(newDegree); + } + + @Override + public void onAccuracyChanged(Sensor sensor, int accuracy) { + // noop + } +} diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java index de7e96e..f1dba43 100644 --- a/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java +++ b/packages/SystemUI/src/com/android/systemui/statusbar/phone/QSTileHost.java @@ -42,6 +42,7 @@ import com.android.systemui.qs.tiles.BluetoothTile; import com.android.systemui.qs.tiles.CastTile; import com.android.systemui.qs.tiles.CellularTile; import com.android.systemui.qs.tiles.ColorInversionTile; +import com.android.systemui.qs.tiles.CompassTile; import com.android.systemui.qs.tiles.CustomQSTile; import com.android.systemui.qs.tiles.DndTile; import com.android.systemui.qs.tiles.EditTile; @@ -330,6 +331,7 @@ public class QSTileHost implements QSTile.Host, Tunable { else if (tileSpec.equals("cast")) return new CastTile(this); else if (tileSpec.equals("hotspot")) return new HotspotTile(this); else if (tileSpec.equals("edit")) return new EditTile(this); + else if (tileSpec.equals("compass")) return new CompassTile(this); else if (tileSpec.startsWith(IntentTile.PREFIX)) return IntentTile.create(this,tileSpec); else throw new IllegalArgumentException("Bad tile spec: " + tileSpec); } @@ -401,6 +403,7 @@ public class QSTileHost implements QSTile.Host, Tunable { else if (spec.equals("hotspot")) return R.string.quick_settings_hotspot_label; else if (spec.equals("edit")) return R.string.quick_settings_edit_label; else if (spec.equals("adb_network")) return R.string.qs_tile_adb_over_network; + else if (spec.equals("compass")) return R.string.qs_tile_compass; return 0; } |