15e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk/*
2340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk * Copyright (C) 2017 The Android Open Source Project
35e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk *
4340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
5340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk * except in compliance with the License. You may obtain a copy of the License at
65e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk *
75e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk *      http://www.apache.org/licenses/LICENSE-2.0
85e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk *
9340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk * Unless required by applicable law or agreed to in writing, software distributed under the
10340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
11340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk * KIND, either express or implied. See the License for the specific language governing
12340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk * permissions and limitations under the License.
135e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk */
14340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk
155e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monkpackage com.android.systemui.tuner;
165e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
175e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monkimport android.app.ActivityManager;
18431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport android.content.BroadcastReceiver;
19431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport android.content.ComponentName;
205e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monkimport android.content.Context;
21431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport android.content.DialogInterface;
22431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport android.content.DialogInterface.OnClickListener;
23431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport android.content.Intent;
24431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport android.content.pm.PackageManager;
25540542b4173d7ce99addfb910d9e73b028ea0f5eJason Monkimport android.content.pm.PackageManager.NameNotFoundException;
26540542b4173d7ce99addfb910d9e73b028ea0f5eJason Monkimport android.os.UserHandle;
275e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monkimport android.provider.Settings;
28c0d7058b14c24cd07912f5629c26b39b7b4673d5Winson
29bcf631d48bed2da32b59d550ee6ea34179fc83c1Evan Lairdimport static android.provider.Settings.System.SHOW_BATTERY_PERCENT;
30431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport com.android.systemui.DemoMode;
31de850bbcaa61c1874b803f2086443febbafd81a4Jason Monkimport com.android.systemui.Dependency;
32431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport com.android.systemui.R;
33431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monkimport com.android.systemui.statusbar.phone.SystemUIDialog;
345e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
35340b0e5216b4fcc435e0459b1ca46155a572100dJason Monkpublic abstract class TunerService {
365e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
37431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk    public static final String ACTION_CLEAR = "com.android.systemui.action.CLEAR_TUNER";
38431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk
39340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract void clearAll();
40340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract void destroy();
417e30d60d1f7adf36b677267efc9991bcf2e97ee2Jason Monk
42340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract String getValue(String setting);
43340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract int getValue(String setting, int def);
44340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract String getValue(String setting, String def);
457e30d60d1f7adf36b677267efc9991bcf2e97ee2Jason Monk
46340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract void setValue(String setting, String value);
47340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract void setValue(String setting, int value);
48f0c6f64e5b6b4d5e635a100d40e2bc5568aaf62bJason Monk
49340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract void addTunable(Tunable tunable, String... keys);
50340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public abstract void removeTunable(Tunable tunable);
51f0c6f64e5b6b4d5e635a100d40e2bc5568aaf62bJason Monk
52340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public interface Tunable {
53340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk        void onTuningChanged(String key, String newValue);
545e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk    }
555e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
56340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    private static Context userContext(Context context) {
57340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk        try {
58340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk            return context.createPackageContextAsUser(context.getPackageName(), 0,
59340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                    new UserHandle(ActivityManager.getCurrentUser()));
60340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk        } catch (NameNotFoundException e) {
61340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk            return context;
625e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk        }
635e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk    }
645e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
65340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public static final void setTunerEnabled(Context context, boolean enabled) {
66340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk        userContext(context).getPackageManager().setComponentEnabledSetting(
67340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                new ComponentName(context, TunerActivity.class),
68340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
69340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                        : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
70340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                PackageManager.DONT_KILL_APP);
715e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
72340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk        userContext(context).getPackageManager().setComponentEnabledSetting(
73340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                new ComponentName(context, TunerActivity.ACTIVITY_ALIAS_NAME),
74340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                enabled ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
75340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                        : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
76340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                PackageManager.DONT_KILL_APP);
775e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk    }
785e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
79340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public static final boolean isTunerEnabled(Context context) {
80340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk        return userContext(context).getPackageManager().getComponentEnabledSetting(
81340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                new ComponentName(context, TunerActivity.class))
82340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
835e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk    }
845e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
85340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk    public static class ClearReceiver extends BroadcastReceiver {
86340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk        @Override
87340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk        public void onReceive(Context context, Intent intent) {
88340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk            if (ACTION_CLEAR.equals(intent.getAction())) {
89340b0e5216b4fcc435e0459b1ca46155a572100dJason Monk                Dependency.get(TunerService.class).clearAll();
905e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk            }
915e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk        }
925e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk    }
935e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk
94431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk    public static final void showResetRequest(final Context context, final Runnable onDisabled) {
95431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk        SystemUIDialog dialog = new SystemUIDialog(context);
96540542b4173d7ce99addfb910d9e73b028ea0f5eJason Monk        dialog.setShowForAllUsers(true);
97431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk        dialog.setMessage(R.string.remove_from_settings_prompt);
98431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk        dialog.setButton(DialogInterface.BUTTON_NEGATIVE, context.getString(R.string.cancel),
99431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                (OnClickListener) null);
100431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk        dialog.setButton(DialogInterface.BUTTON_POSITIVE,
101431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                context.getString(R.string.guest_exit_guest_dialog_remove), new OnClickListener() {
102431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk            @Override
103431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk            public void onClick(DialogInterface dialog, int which) {
104431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                // Tell the tuner (in main SysUI process) to clear all its settings.
105431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                context.sendBroadcast(new Intent(TunerService.ACTION_CLEAR));
106431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                // Disable access to tuner.
107431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                TunerService.setTunerEnabled(context, false);
108431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                // Make them sit through the warning dialog again.
109431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                Settings.Secure.putInt(context.getContentResolver(),
110431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                        TunerFragment.SETTING_SEEN_TUNER_WARNING, 0);
111431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                if (onDisabled != null) {
112431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                    onDisabled.run();
113431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk                }
114431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk            }
115431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk        });
116431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk        dialog.show();
117431ad737a4752e9325894211c28bb0f0f97c00a7Jason Monk    }
1185e745172d93fcd4a90e0c8b216941a10a9994de8Jason Monk}
119