summaryrefslogtreecommitdiffstats
path: root/src/com/cyanogenmod/setupwizard/setup/OtherSettingsPage.java
blob: 5c0b91d06b3e87351a98162f7fbc92afaad6a892 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (C) 2015 The CyanogenMod Project
 * Copyright (C) 2017 The LineageOS 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.cyanogenmod.setupwizard.setup;

import com.cyanogenmod.setupwizard.R;
import com.cyanogenmod.setupwizard.cmstats.SetupStats;
import com.cyanogenmod.setupwizard.ui.SetupPageFragment;
import com.cyanogenmod.setupwizard.util.SetupWizardUtils;

import android.app.Fragment;
import android.app.FragmentManager;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class OtherSettingsPage extends SetupPage {

    public static final String TAG = "OtherSettingsPage";

    public OtherSettingsPage(Context context, SetupDataCallbacks callbacks) {
        super(context, callbacks);
    }

    @Override
    public Fragment getFragment(FragmentManager fragmentManager, int action) {
        Fragment fragment = fragmentManager.findFragmentByTag(getKey());
        if (fragment == null) {
            Bundle args = new Bundle();
            args.putString(Page.KEY_PAGE_ARGUMENT, getKey());
            args.putInt(Page.KEY_PAGE_ACTION, action);
            fragment = new OtherSettingsFragment();
            fragment.setArguments(args);
        }
        return fragment;
    }

    @Override
    public String getKey() {
        return TAG;
    }

    @Override
    public int getTitleResId() {
        return R.string.setup_location;
    }

    @Override
    public int getIconResId() {
        return R.drawable.ic_location;
    }

    public static class OtherSettingsFragment extends SetupPageFragment {

        private View mLocationRow;
        private View mBatteryRow;
        private View mNetworkRow;
        private CheckBox mNetwork;
        private CheckBox mBattery;
        private CheckBox mLocationAccess;

        private ContentResolver mContentResolver;


        /** Broadcast intent action when the location mode is about to change. */
        private static final String MODE_CHANGING_ACTION =
                "com.android.settings.location.MODE_CHANGING";
        private static final String CURRENT_MODE_KEY = "CURRENT_MODE";
        private static final String NEW_MODE_KEY = "NEW_MODE";

        private int mCurrentMode = Settings.Secure.LOCATION_MODE_OFF;
        private BroadcastReceiver mReceiver;


        private View.OnClickListener mLocationClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onToggleLocationAccess(!mLocationAccess.isChecked());
            }
        };

        private View.OnClickListener mBatteryClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onToggleBatterySaving(!mBattery.isChecked());
            }
        };

        private View.OnClickListener mNetworkClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onToggleNetwork(!mNetwork.isChecked());
            }
        };

        @Override
        protected void initializePage() {
            final boolean hasTelephony = SetupWizardUtils.hasTelephony(getActivity());
            mContentResolver = getActivity().getContentResolver();
            TextView summaryView = (TextView) mRootView.findViewById(android.R.id.summary);
            summaryView.setText(R.string.location_services_summary);
            mLocationRow = mRootView.findViewById(R.id.location);
            mLocationRow.setOnClickListener(mLocationClickListener);
            mLocationAccess = (CheckBox) mRootView.findViewById(R.id.location_checkbox);
            mBatteryRow = mRootView.findViewById(R.id.battery_saving);
            mBatteryRow.setOnClickListener(mBatteryClickListener);
            mBattery = (CheckBox) mRootView.findViewById(R.id.battery_saving_checkbox);
            mNetworkRow = mRootView.findViewById(R.id.network);
            mNetworkRow.setOnClickListener(mNetworkClickListener);
            mNetwork = (CheckBox) mRootView.findViewById(R.id.network_checkbox);
            TextView networkSummary = (TextView) mRootView.findViewById(R.id.network_summary);
            if (hasTelephony) {
                networkSummary.setText(R.string.location_network_telephony);
            } else {
                networkSummary.setText(R.string.location_network);
            }
        }

        @Override
        protected int getLayoutResource() {
            return R.layout.location_settings;
        }

        @Override
        public void onResume() {
            super.onResume();
            refreshLocationMode();
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (Log.isLoggable(TAG, Log.DEBUG)) {
                        Log.d(TAG, "Received location mode change intent: " + intent);
                    }
                    refreshLocationMode();
                }
            };
        }

        private void setLocationMode(int mode) {
            Intent intent = new Intent(MODE_CHANGING_ACTION);
            intent.putExtra(CURRENT_MODE_KEY, mCurrentMode);
            intent.putExtra(NEW_MODE_KEY, mode);
            getActivity().sendBroadcast(intent, android.Manifest.permission.WRITE_SECURE_SETTINGS);
            Settings.Secure.putInt(mContentResolver, Settings.Secure.LOCATION_MODE, mode);
            refreshLocationMode();
        }

        private void refreshLocationMode() {
            int mode = Settings.Secure.getInt(mContentResolver, Settings.Secure.LOCATION_MODE,
                    Settings.Secure.LOCATION_MODE_OFF);

            if (mCurrentMode != mode) {
                mCurrentMode = mode;
                if (Log.isLoggable(TAG, Log.INFO)) {
                    Log.i(TAG, "Location mode has been changed");
                }
                updateLocationToggles(mode);
            }
        }

        private void updateLocationToggles(int mode) {
            switch (mode) {
                case Settings.Secure.LOCATION_MODE_OFF:
                    mLocationAccess.setChecked(false);
                    mBattery.setChecked(false);
                    mBattery.setEnabled(false);
                    mBatteryRow.setEnabled(false);
                    mNetwork.setChecked(false);
                    mNetwork.setEnabled(false);
                    mNetworkRow.setEnabled(false);
                    break;
                case Settings.Secure.LOCATION_MODE_SENSORS_ONLY:
                    mLocationAccess.setChecked(true);
                    mBattery.setChecked(false);
                    mBattery.setEnabled(true);
                    mBatteryRow.setEnabled(true);
                    mNetwork.setChecked(false);
                    mNetwork.setEnabled(true);
                    mNetworkRow.setEnabled(true);
                    break;
                case Settings.Secure.LOCATION_MODE_BATTERY_SAVING:
                    mLocationAccess.setChecked(true);
                    mBattery.setChecked(true);
                    mNetwork.setChecked(false);
                    mNetwork.setEnabled(false);
                    mNetworkRow.setEnabled(false);
                    break;
                case Settings.Secure.LOCATION_MODE_HIGH_ACCURACY:
                    mLocationAccess.setChecked(true);
                    mNetwork.setChecked(true);
                    mBattery.setChecked(false);
                    mBattery.setEnabled(false);
                    mBatteryRow.setEnabled(false);
                    break;
                default:
                    mLocationAccess.setChecked(false);
                    mBattery.setChecked(false);
                    mBattery.setEnabled(false);
                    mBatteryRow.setEnabled(false);
                    mNetwork.setChecked(false);
                    mNetwork.setEnabled(false);
                    mNetworkRow.setEnabled(false);
                    break;
            }
        }

        private void onToggleLocationAccess(boolean checked) {
            SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED,
                    SetupStats.Action.ENABLE_LOCATION,
                    SetupStats.Label.CHECKED, String.valueOf(checked));

            if (checked) {
                setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
            } else {
                setLocationMode(Settings.Secure.LOCATION_MODE_OFF);
            }
        }

        private void onToggleBatterySaving(boolean checked) {
            /* SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED,
                    SetupStats.Action.ENABLE_BATTERY_SAVING_LOCATION,
                    SetupStats.Label.CHECKED, String.valueOf(checked)); */

            if (checked) {
                setLocationMode(Settings.Secure.LOCATION_MODE_BATTERY_SAVING);
            } else {
                setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
            }
        }

        private void onToggleNetwork(boolean checked) {
            SetupStats.addEvent(SetupStats.Categories.SETTING_CHANGED,
                    SetupStats.Action.ENABLE_NETWORK_LOCATION,
                    SetupStats.Label.CHECKED, String.valueOf(checked));

            if (checked) {
                setLocationMode(Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
            } else {
                setLocationMode(Settings.Secure.LOCATION_MODE_SENSORS_ONLY);
            }
        }
    }
}