Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 3187e88f authored by Roman Birg's avatar Roman Birg Committed by Danesh M
Browse files

SystemUI: port compass tile to cm-13.0

Change-Id: I4cce9c849a702947c0cf00e3e1293f89c985b9e9
parent 6be220df
Loading
Loading
Loading
Loading
+32 −0
Original line number Diff line number Diff line
<?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
+31 −0
Original line number Diff line number Diff line
<?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
+12 −0
Original line number Diff line number Diff line
@@ -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>
+4 −0
Original line number Diff line number Diff line
@@ -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>
+193 −0
Original line number Diff line number Diff line
/*
 * 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
    }
}
Loading