• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2020 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.google.android.car.kitchensink.rotary;
18 
19 import android.app.ActivityManager;
20 import android.content.BroadcastReceiver;
21 import android.content.ComponentName;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.content.IntentFilter;
25 import android.content.pm.ApplicationInfo;
26 import android.content.pm.PackageManager;
27 import android.os.Bundle;
28 import android.provider.Settings;
29 import android.util.Log;
30 import android.view.LayoutInflater;
31 import android.view.View;
32 import android.view.ViewGroup;
33 import android.widget.Button;
34 import android.widget.Switch;
35 import android.widget.TextView;
36 
37 import androidx.annotation.NonNull;
38 import androidx.annotation.Nullable;
39 import androidx.fragment.app.Fragment;
40 
41 import com.google.android.car.kitchensink.R;
42 
43 import java.util.HashSet;
44 import java.util.Set;
45 
46 /** Test fragment to enable/disable various components related to RotaryController. */
47 public final class RotaryFragment extends Fragment {
48 
49     private static final String TAG = RotaryFragment.class.getSimpleName();
50     private static final String ROTARY_CONTROLLER_PACKAGE = "com.android.car.rotary";
51     private static final ComponentName ROTARY_SERVICE_NAME = ComponentName.unflattenFromString(
52             ROTARY_CONTROLLER_PACKAGE + "/com.android.car.rotary.RotaryService");
53     private static final String ROTARY_PLAYGROUND_PACKAGE = "com.android.car.rotaryplayground";
54     private static final String ROTARY_IME_PACKAGE = "com.android.car.rotaryime";
55 
56     private static final String ACCESSIBILITY_DELIMITER = ":";
57 
58     private static final int ON = 1;
59     private static final int OFF = 0;
60 
61     private final IntentFilter mFilter = new IntentFilter();
62 
63     private TextView mUnavailableMessage;
64     private Button mEnableAllButton;
65     private Button mDisableAllButton;
66     private Switch mRotaryServiceToggle;
67     private Switch mRotaryImeToggle;
68     private Switch mRotaryPlaygroundToggle;
69 
70     private final BroadcastReceiver mPackagesUpdatedReceiver = new BroadcastReceiver() {
71         @Override
72         public void onReceive(Context context, Intent intent) {
73             String action = intent.getAction();
74             switch (action) {
75                 case Intent.ACTION_PACKAGE_ADDED:
76                 case Intent.ACTION_PACKAGE_CHANGED:
77                 case Intent.ACTION_PACKAGE_REMOVED:
78                     refreshUi();
79                     break;
80             }
81         }
82     };
83 
84     @Override
onCreate(@ullable Bundle savedInstanceState)85     public void onCreate(@Nullable Bundle savedInstanceState) {
86         super.onCreate(savedInstanceState);
87 
88         mFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
89         mFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
90         mFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
91     }
92 
93     @Nullable
94     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)95     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
96             @Nullable Bundle savedInstanceState) {
97         return inflater.inflate(R.layout.rotary_fragment, container, /* attachToRoot= */ false);
98     }
99 
100     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)101     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
102         super.onViewCreated(view, savedInstanceState);
103 
104         mUnavailableMessage = view.findViewById(R.id.unavailable_message);
105 
106         mEnableAllButton = view.findViewById(R.id.enable_all_rotary);
107         mDisableAllButton = view.findViewById(R.id.disable_all_rotary);
108 
109         mRotaryServiceToggle = view.findViewById(R.id.rotary_service_toggle);
110         mRotaryImeToggle = view.findViewById(R.id.rotary_ime_toggle);
111         mRotaryPlaygroundToggle = view.findViewById(R.id.rotary_playground_toggle);
112 
113         mRotaryServiceToggle.setOnCheckedChangeListener(
114                 (buttonView, isChecked) -> enableRotaryService(getContext(), isChecked));
115         mRotaryImeToggle.setOnCheckedChangeListener(
116                 (buttonView, isChecked) -> enableApplication(getContext(), ROTARY_IME_PACKAGE,
117                         isChecked));
118         mRotaryPlaygroundToggle.setOnCheckedChangeListener(
119                 (buttonView, isChecked) -> enableApplication(getContext(),
120                         ROTARY_PLAYGROUND_PACKAGE, isChecked));
121 
122         mEnableAllButton.setOnClickListener(v -> {
123             if (mRotaryServiceToggle.isEnabled()) {
124                 mRotaryServiceToggle.setChecked(true);
125             }
126             if (mRotaryImeToggle.isEnabled()) {
127                 mRotaryImeToggle.setChecked(true);
128             }
129             if (mRotaryPlaygroundToggle.isEnabled()) {
130                 mRotaryPlaygroundToggle.setChecked(true);
131             }
132         });
133 
134         mDisableAllButton.setOnClickListener(v -> {
135             if (mRotaryServiceToggle.isEnabled()) {
136                 mRotaryServiceToggle.setChecked(false);
137             }
138             if (mRotaryImeToggle.isEnabled()) {
139                 mRotaryImeToggle.setChecked(false);
140             }
141             if (mRotaryPlaygroundToggle.isEnabled()) {
142                 mRotaryPlaygroundToggle.setChecked(false);
143             }
144         });
145     }
146 
147     @Override
onStart()148     public void onStart() {
149         super.onStart();
150 
151         getContext().registerReceiver(mPackagesUpdatedReceiver, mFilter,
152                 Context.RECEIVER_NOT_EXPORTED);
153     }
154 
155     @Override
onResume()156     public void onResume() {
157         super.onResume();
158 
159         refreshUi();
160     }
161 
refreshUi()162     private void refreshUi() {
163         ApplicationInfo info = findApplication(getContext(), ROTARY_CONTROLLER_PACKAGE);
164         boolean rotaryApplicationExists = info != null;
165         info = findApplication(getContext(), ROTARY_IME_PACKAGE);
166         boolean rotaryImeExists = info != null;
167         info = findApplication(getContext(), ROTARY_PLAYGROUND_PACKAGE);
168         boolean rotaryPlaygroundExists = info != null;
169 
170         mUnavailableMessage.setVisibility(!rotaryApplicationExists ? View.VISIBLE : View.GONE);
171 
172         mEnableAllButton.setEnabled(rotaryApplicationExists);
173         mDisableAllButton.setEnabled(rotaryApplicationExists);
174         mRotaryServiceToggle.setEnabled(rotaryApplicationExists);
175         mRotaryImeToggle.setEnabled(rotaryApplicationExists && rotaryImeExists);
176         mRotaryPlaygroundToggle.setEnabled(rotaryApplicationExists && rotaryPlaygroundExists);
177 
178         mRotaryServiceToggle.setChecked(isRotaryServiceEnabled(getContext()));
179         mRotaryImeToggle.setChecked(
180                 rotaryImeExists && isApplicationEnabled(getContext(), ROTARY_IME_PACKAGE));
181         mRotaryPlaygroundToggle.setChecked(
182                 rotaryPlaygroundExists && isApplicationEnabled(getContext(),
183                         ROTARY_PLAYGROUND_PACKAGE));
184     }
185 
186     @Override
onStop()187     public void onStop() {
188         super.onStop();
189 
190         getContext().unregisterReceiver(mPackagesUpdatedReceiver);
191     }
192 
193     @Nullable
findApplication(Context context, String packageName)194     private static ApplicationInfo findApplication(Context context, String packageName) {
195         PackageManager pm = context.getPackageManager();
196         try {
197             Log.d(TAG, "Searching for: " + packageName);
198             return pm.getApplicationInfoAsUser(packageName, /* flags= */ 0,
199                     ActivityManager.getCurrentUser());
200         } catch (PackageManager.NameNotFoundException e) {
201             Log.e(TAG, "Could not find: " + packageName);
202             return null;
203         }
204     }
205 
isApplicationEnabled(Context context, String packageName)206     private static boolean isApplicationEnabled(Context context, String packageName) {
207         ApplicationInfo info = findApplication(context, packageName);
208         if (info == null) {
209             return false;
210         }
211 
212         return info.enabled;
213     }
214 
enableApplication(Context context, String packageName, boolean enable)215     private static void enableApplication(Context context, String packageName, boolean enable) {
216         // Check that the application exists.
217         ApplicationInfo info = findApplication(context, packageName);
218         if (info == null) {
219             Log.e(TAG, "Cannot enable application. " + packageName + " package does not exist");
220             return;
221         }
222 
223         PackageManager pm = context.getPackageManager();
224         int currentState = pm.getApplicationEnabledSetting(packageName);
225         int desiredState = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
226                 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
227 
228         boolean isEnabled = currentState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
229         if (isEnabled != enable
230                 || currentState == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
231             Log.d(TAG, "Application state updated for " + packageName + ": " + enable);
232             pm.setApplicationEnabledSetting(packageName, desiredState, /* flags= */ 0);
233         }
234     }
235 
isRotaryServiceEnabled(Context context)236     private static boolean isRotaryServiceEnabled(Context context) {
237         ApplicationInfo info = findApplication(context, ROTARY_CONTROLLER_PACKAGE);
238         if (info == null) {
239             return false;
240         }
241 
242         int isAccessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(),
243                 Settings.Secure.ACCESSIBILITY_ENABLED, OFF);
244         if (isAccessibilityEnabled != ON) {
245             return false;
246         }
247 
248         String services = Settings.Secure.getString(context.getContentResolver(),
249                 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
250         if (services == null) {
251             return false;
252         }
253 
254         return services.contains(ROTARY_CONTROLLER_PACKAGE);
255     }
256 
enableRotaryService(Context context, boolean enable)257     private static void enableRotaryService(Context context, boolean enable) {
258         // Check that the RotaryController exists.
259         ApplicationInfo info = findApplication(context, ROTARY_CONTROLLER_PACKAGE);
260         if (info == null) {
261             Log.e(TAG, "Cannot enable rotary service. " + ROTARY_CONTROLLER_PACKAGE
262                     + " package does not exist");
263             return;
264         }
265 
266         // Set the list of accessibility services.
267         String services = Settings.Secure.getString(context.getContentResolver(),
268                 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
269         if (services == null) {
270             services = "";
271         }
272         Log.d(TAG, "Current list of accessibility services: " + services);
273 
274         String[] servicesArray = services.split(ACCESSIBILITY_DELIMITER);
275         Set<ComponentName> servicesSet = new HashSet<>();
276         for (String service : servicesArray) {
277             ComponentName name = ComponentName.unflattenFromString(service);
278             if (name != null) {
279                 servicesSet.add(name);
280             }
281         }
282 
283         if (enable) {
284             servicesSet.add(ROTARY_SERVICE_NAME);
285         } else {
286             servicesSet.remove(ROTARY_SERVICE_NAME);
287         }
288 
289         StringBuilder builder = new StringBuilder();
290         for (ComponentName service : servicesSet) {
291             if (builder.length() > 0) {
292                 builder.append(ACCESSIBILITY_DELIMITER);
293             }
294             builder.append(service.flattenToString());
295         }
296 
297         Log.d(TAG, "New list of accessibility services: " + builder);
298         Settings.Secure.putString(context.getContentResolver(),
299                 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
300                 builder.toString());
301 
302         // Set the overall enabled state.
303         int desiredState = enable ? ON : OFF;
304         Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED,
305                 desiredState);
306     }
307 }
308