1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.egg;
18 
19 import android.app.Activity;
20 import android.content.ComponentName;
21 import android.content.Context;
22 import android.content.pm.PackageManager;
23 import android.provider.Settings;
24 import android.util.Log;
25 import android.widget.Toast;
26 
27 import com.android.egg.flags.Flags;
28 import com.android.egg.landroid.DreamUniverse;
29 import com.android.egg.neko.NekoControlsService;
30 import com.android.egg.widget.PaintChipsActivity;
31 import com.android.egg.widget.PaintChipsWidget;
32 
33 /**
34  * Launched from the PlatLogoActivity. Enables everything else in this easter egg.
35  */
36 public class ComponentActivationActivity extends Activity {
37     private static final String TAG = "EasterEgg";
38 
39     // check PlatLogoActivity.java for these
40     private static final String S_EGG_UNLOCK_SETTING = "egg_mode_s";
41     private static final String V_EGG_UNLOCK_SETTING = "egg_mode_v";
42 
toastUp(String s)43     private void toastUp(String s) {
44         Toast toast = Toast.makeText(this, s, Toast.LENGTH_SHORT);
45         toast.show();
46     }
47 
48     @Override
onStart()49     public void onStart() {
50         super.onStart();
51 
52         lockUnlockComponents(this);
53 
54         finish();
55     }
56 
57     /**
58      * Check easter egg unlock state and update unlockable components to match.
59      */
lockUnlockComponents(Context context)60     public static void lockUnlockComponents(Context context) {
61         final PackageManager pm = context.getPackageManager();
62         final ComponentName[] cns;
63         final String unlockSettingsKey;
64         final boolean shouldReLock;
65         final long unlockValue;
66         if (Flags.flagFlag()) {
67             unlockSettingsKey = V_EGG_UNLOCK_SETTING;
68             unlockValue = 1; // since we're not toggling we actually don't need to check the setting
69             shouldReLock = false;
70             cns = new ComponentName[]{
71                     new ComponentName(context, DreamUniverse.class)
72             };
73         } else {
74             unlockSettingsKey = S_EGG_UNLOCK_SETTING;
75             unlockValue = Settings.System.getLong(context.getContentResolver(),
76                     unlockSettingsKey, 0);
77             shouldReLock = true;
78             cns = new ComponentName[]{
79                     new ComponentName(context, NekoControlsService.class),
80                     new ComponentName(context, PaintChipsActivity.class),
81                     new ComponentName(context, PaintChipsWidget.class),
82                     new ComponentName(context, DreamUniverse.class)
83             };
84         }
85         for (ComponentName cn : cns) {
86             final boolean componentEnabled = pm.getComponentEnabledSetting(cn)
87                     == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
88             if (unlockValue == 0) {
89                 if (componentEnabled) {
90                     Log.v(TAG, "Disabling component: " + cn);
91                     pm.setComponentEnabledSetting(cn,
92                             PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
93                             PackageManager.DONT_KILL_APP);
94                     //toastUp("\uD83D\uDEAB");
95                 } else {
96                     Log.v(TAG, "Already disabled: " + cn);
97                 }
98             } else {
99                 if (!componentEnabled) {
100                     Log.v(TAG, "Enabling component: " + cn);
101                     pm.setComponentEnabledSetting(cn,
102                             PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
103                             PackageManager.DONT_KILL_APP);
104                     //toastUp("\uD83D\uDC31");
105                 } else {
106                     Log.v(TAG, "Already enabled: " + cn);
107                 }
108             }
109         }
110     }
111 }
112