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

Commit f5994c83 authored by Danesh M's avatar Danesh M
Browse files

Prompt permission dialog for status bar weather

CYNGNOS-2222

Change-Id: I435b1badbc5949cb0bc76050a67e212d519c466a
parent 6b5c6f33
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -37,10 +37,11 @@

        <!-- show weather -->
        <com.android.systemui.qs.QSBooleanSettingRow
                android:id="@+id/show_weather"
                style="@style/SettingRow"
                android:key="status_bar_show_weather"
                android:title="@string/quick_settings_title_show_weather"
                systemui:defaultValue="1"
                systemui:defaultValue="0"
                systemui:table="cm_system"/>

        <!-- brightness slider -->
+15 −0
Original line number Diff line number Diff line
@@ -49,6 +49,7 @@ public class QSBooleanSettingRow extends LinearLayout implements View.OnClickLis
    private TextView mText;
    private Switch mSwitch;
    private int mDefaultValue;
    private CompoundButton.OnCheckedChangeListener mOnCheckedChangeListener;

    public QSBooleanSettingRow(Context context) {
        this(context, null);
@@ -99,6 +100,9 @@ public class QSBooleanSettingRow extends LinearLayout implements View.OnClickLis
                            + "buttonView = [" + buttonView + "], isChecked = [" + isChecked
                            + "] and table: " + mWhichTable + ", and key: " + mKey);
                    applyChange(isChecked);
                    if (mOnCheckedChangeListener != null) {
                        mOnCheckedChangeListener.onCheckedChanged(buttonView, isChecked);
                    }
                }
            });
        }
@@ -106,6 +110,13 @@ public class QSBooleanSettingRow extends LinearLayout implements View.OnClickLis
        a.recycle();
    }

    public void setChecked(boolean checked) {
        if (mSwitch.isChecked() == checked) {
            return;
        }
        mSwitch.setChecked(checked);
    }

    private void applyChange(boolean value) {
        ContentResolver cr = getContext().getContentResolver();
        switch (mWhichTable) {
@@ -160,4 +171,8 @@ public class QSBooleanSettingRow extends LinearLayout implements View.OnClickLis
    public void onClick(View v) {
        mSwitch.setChecked(!mSwitch.isChecked());
    }

    public void setOnCheckedChangeListener(CompoundButton.OnCheckedChangeListener l) {
        mOnCheckedChangeListener = l;
    }
}
+62 −3
Original line number Diff line number Diff line
@@ -15,15 +15,22 @@
 */
package com.android.systemui.qs;

import android.Manifest;
import android.annotation.Nullable;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.ResultReceiver;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.CompoundButton;
import android.widget.ScrollView;

import com.android.systemui.R;
@@ -32,9 +39,16 @@ import com.android.systemui.statusbar.phone.SystemUIDialog;

public class QSSettings extends ScrollView {

    private static final String RESULT_RECEIVER_EXTRA = "result_receiver";
    private static final String LOCK_CLOCK_PACKAGENAME = "com.cyanogenmod.lockclock";
    private static final String LOCK_CLOCK_PERM_CLASS = LOCK_CLOCK_PACKAGENAME
            + ".weather.PermissionRequestActivity";

    private QSTileHost mHost;

    private boolean mAdapterEditingState;
    private QSBooleanSettingRow mShowWeather;
    private ResultReceiver mResultReceiver;

    public QSSettings(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
@@ -51,6 +65,51 @@ public class QSSettings extends ScrollView {
                initiateTileReset();
            }
        });

        mShowWeather = (QSBooleanSettingRow) findViewById(R.id.show_weather);
        mShowWeather.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    PackageManager packageManager = getContext().getPackageManager();
                    if (packageManager.checkPermission(Manifest.permission.ACCESS_COARSE_LOCATION,
                            LOCK_CLOCK_PACKAGENAME) != PackageManager.PERMISSION_GRANTED) {
                        mShowWeather.setChecked(false);
                        requestPermission();
                        mHost.collapsePanels();
                    }
                }
            }
        });
    }

    public Parcelable getResultReceiverForSending() {
        if (mResultReceiver == null) {
            mResultReceiver = new ResultReceiver(new Handler()) {
                @Override
                protected void onReceiveResult(int resultCode, Bundle resultData) {
                    super.onReceiveResult(resultCode, resultData);
                    if (resultCode == Activity.RESULT_OK) {
                        mShowWeather.setChecked(true);
                    }
                    mResultReceiver = null;
                }
            };
        }
        Parcel parcel = Parcel.obtain();
        mResultReceiver.writeToParcel(parcel, 0);
        parcel.setDataPosition(0);
        ResultReceiver receiverForSending = ResultReceiver.CREATOR.createFromParcel(parcel);
        parcel.recycle();
        return receiverForSending;
    }

    private void requestPermission() {
        Intent i = new Intent();
        i.setClassName(LOCK_CLOCK_PACKAGENAME, LOCK_CLOCK_PERM_CLASS);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.putExtra(RESULT_RECEIVER_EXTRA, getResultReceiverForSending());
        getContext().startActivity(i);
    }

    private void initiateTileReset() {