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                 default:
81                     throw new IllegalArgumentException("Invalid action: " + action);
82             }
83         }
84     };
85 
86     @Override
onCreate(@ullable Bundle savedInstanceState)87     public void onCreate(@Nullable Bundle savedInstanceState) {
88         super.onCreate(savedInstanceState);
89 
90         mFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
91         mFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
92         mFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
93     }
94 
95     @Nullable
96     @Override
onCreateView(@onNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)97     public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
98             @Nullable Bundle savedInstanceState) {
99         return inflater.inflate(R.layout.rotary_fragment, container, /* attachToRoot= */ false);
100     }
101 
102     @Override
onViewCreated(@onNull View view, @Nullable Bundle savedInstanceState)103     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
104         super.onViewCreated(view, savedInstanceState);
105 
106         mUnavailableMessage = view.findViewById(R.id.unavailable_message);
107 
108         mEnableAllButton = view.findViewById(R.id.enable_all_rotary);
109         mDisableAllButton = view.findViewById(R.id.disable_all_rotary);
110 
111         mRotaryServiceToggle = view.findViewById(R.id.rotary_service_toggle);
112         mRotaryImeToggle = view.findViewById(R.id.rotary_ime_toggle);
113         mRotaryPlaygroundToggle = view.findViewById(R.id.rotary_playground_toggle);
114 
115         mRotaryServiceToggle.setOnCheckedChangeListener(
116                 (buttonView, isChecked) -> enableRotaryService(getContext(), isChecked));
117         mRotaryImeToggle.setOnCheckedChangeListener(
118                 (buttonView, isChecked) -> enableApplication(getContext(), ROTARY_IME_PACKAGE,
119                         isChecked));
120         mRotaryPlaygroundToggle.setOnCheckedChangeListener(
121                 (buttonView, isChecked) -> enableApplication(getContext(),
122                         ROTARY_PLAYGROUND_PACKAGE, isChecked));
123 
124         mEnableAllButton.setOnClickListener(v -> {
125             if (mRotaryServiceToggle.isEnabled()) {
126                 mRotaryServiceToggle.setChecked(true);
127             }
128             if (mRotaryImeToggle.isEnabled()) {
129                 mRotaryImeToggle.setChecked(true);
130             }
131             if (mRotaryPlaygroundToggle.isEnabled()) {
132                 mRotaryPlaygroundToggle.setChecked(true);
133             }
134         });
135 
136         mDisableAllButton.setOnClickListener(v -> {
137             if (mRotaryServiceToggle.isEnabled()) {
138                 mRotaryServiceToggle.setChecked(false);
139             }
140             if (mRotaryImeToggle.isEnabled()) {
141                 mRotaryImeToggle.setChecked(false);
142             }
143             if (mRotaryPlaygroundToggle.isEnabled()) {
144                 mRotaryPlaygroundToggle.setChecked(false);
145             }
146         });
147     }
148 
149     @Override
onStart()150     public void onStart() {
151         super.onStart();
152 
153         getContext().registerReceiver(mPackagesUpdatedReceiver, mFilter,
154                 Context.RECEIVER_NOT_EXPORTED);
155     }
156 
157     @Override
onResume()158     public void onResume() {
159         super.onResume();
160 
161         refreshUi();
162     }
163 
refreshUi()164     private void refreshUi() {
165         ApplicationInfo info = findApplication(getContext(), ROTARY_CONTROLLER_PACKAGE);
166         boolean rotaryApplicationExists = info != null;
167         info = findApplication(getContext(), ROTARY_IME_PACKAGE);
168         boolean rotaryImeExists = info != null;
169         info = findApplication(getContext(), ROTARY_PLAYGROUND_PACKAGE);
170         boolean rotaryPlaygroundExists = info != null;
171 
172         mUnavailableMessage.setVisibility(!rotaryApplicationExists ? View.VISIBLE : View.GONE);
173 
174         mEnableAllButton.setEnabled(rotaryApplicationExists);
175         mDisableAllButton.setEnabled(rotaryApplicationExists);
176         mRotaryServiceToggle.setEnabled(rotaryApplicationExists);
177         mRotaryImeToggle.setEnabled(rotaryApplicationExists && rotaryImeExists);
178         mRotaryPlaygroundToggle.setEnabled(rotaryApplicationExists && rotaryPlaygroundExists);
179 
180         mRotaryServiceToggle.setChecked(isRotaryServiceEnabled(getContext()));
181         mRotaryImeToggle.setChecked(
182                 rotaryImeExists && isApplicationEnabled(getContext(), ROTARY_IME_PACKAGE));
183         mRotaryPlaygroundToggle.setChecked(
184                 rotaryPlaygroundExists && isApplicationEnabled(getContext(),
185                         ROTARY_PLAYGROUND_PACKAGE));
186     }
187 
188     @Override
onStop()189     public void onStop() {
190         super.onStop();
191 
192         getContext().unregisterReceiver(mPackagesUpdatedReceiver);
193     }
194 
195     @Nullable
findApplication(Context context, String packageName)196     private static ApplicationInfo findApplication(Context context, String packageName) {
197         PackageManager pm = context.getPackageManager();
198         try {
199             Log.d(TAG, "Searching for: " + packageName);
200             return pm.getApplicationInfoAsUser(packageName, /* flags= */ 0,
201                     ActivityManager.getCurrentUser());
202         } catch (PackageManager.NameNotFoundException e) {
203             Log.e(TAG, "Could not find: " + packageName);
204             return null;
205         }
206     }
207 
isApplicationEnabled(Context context, String packageName)208     private static boolean isApplicationEnabled(Context context, String packageName) {
209         ApplicationInfo info = findApplication(context, packageName);
210         if (info == null) {
211             return false;
212         }
213 
214         return info.enabled;
215     }
216 
enableApplication(Context context, String packageName, boolean enable)217     private static void enableApplication(Context context, String packageName, boolean enable) {
218         // Check that the application exists.
219         ApplicationInfo info = findApplication(context, packageName);
220         if (info == null) {
221             Log.e(TAG, "Cannot enable application. " + packageName + " package does not exist");
222             return;
223         }
224 
225         PackageManager pm = context.getPackageManager();
226         int currentState = pm.getApplicationEnabledSetting(packageName);
227         int desiredState = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
228                 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED;
229 
230         boolean isEnabled = currentState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
231         if (isEnabled != enable
232                 || currentState == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) {
233             Log.d(TAG, "Application state updated for " + packageName + ": " + enable);
234             pm.setApplicationEnabledSetting(packageName, desiredState, /* flags= */ 0);
235         }
236     }
237 
isRotaryServiceEnabled(Context context)238     private static boolean isRotaryServiceEnabled(Context context) {
239         ApplicationInfo info = findApplication(context, ROTARY_CONTROLLER_PACKAGE);
240         if (info == null) {
241             return false;
242         }
243 
244         int isAccessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(),
245                 Settings.Secure.ACCESSIBILITY_ENABLED, OFF);
246         if (isAccessibilityEnabled != ON) {
247             return false;
248         }
249 
250         String services = Settings.Secure.getString(context.getContentResolver(),
251                 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
252         if (services == null) {
253             return false;
254         }
255 
256         return services.contains(ROTARY_CONTROLLER_PACKAGE);
257     }
258 
enableRotaryService(Context context, boolean enable)259     private static void enableRotaryService(Context context, boolean enable) {
260         // Check that the RotaryController exists.
261         ApplicationInfo info = findApplication(context, ROTARY_CONTROLLER_PACKAGE);
262         if (info == null) {
263             Log.e(TAG, "Cannot enable rotary service. " + ROTARY_CONTROLLER_PACKAGE
264                     + " package does not exist");
265             return;
266         }
267 
268         // Set the list of accessibility services.
269         String services = Settings.Secure.getString(context.getContentResolver(),
270                 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES);
271         if (services == null) {
272             services = "";
273         }
274         Log.d(TAG, "Current list of accessibility services: " + services);
275 
276         String[] servicesArray = services.split(ACCESSIBILITY_DELIMITER);
277         Set<ComponentName> servicesSet = new HashSet<>();
278         for (String service : servicesArray) {
279             ComponentName name = ComponentName.unflattenFromString(service);
280             if (name != null) {
281                 servicesSet.add(name);
282             }
283         }
284 
285         if (enable) {
286             servicesSet.add(ROTARY_SERVICE_NAME);
287         } else {
288             servicesSet.remove(ROTARY_SERVICE_NAME);
289         }
290 
291         StringBuilder builder = new StringBuilder();
292         for (ComponentName service : servicesSet) {
293             if (builder.length() > 0) {
294                 builder.append(ACCESSIBILITY_DELIMITER);
295             }
296             builder.append(service.flattenToString());
297         }
298 
299         Log.d(TAG, "New list of accessibility services: " + builder);
300         Settings.Secure.putString(context.getContentResolver(),
301                 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES,
302                 builder.toString());
303 
304         // Set the overall enabled state.
305         int desiredState = enable ? ON : OFF;
306         Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED,
307                 desiredState);
308     }
309 }
310