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 } 153 154 @Override onResume()155 public void onResume() { 156 super.onResume(); 157 158 refreshUi(); 159 } 160 refreshUi()161 private void refreshUi() { 162 ApplicationInfo info = findApplication(getContext(), ROTARY_CONTROLLER_PACKAGE); 163 boolean rotaryApplicationExists = info != null; 164 info = findApplication(getContext(), ROTARY_IME_PACKAGE); 165 boolean rotaryImeExists = info != null; 166 info = findApplication(getContext(), ROTARY_PLAYGROUND_PACKAGE); 167 boolean rotaryPlaygroundExists = info != null; 168 169 mUnavailableMessage.setVisibility(!rotaryApplicationExists ? View.VISIBLE : View.GONE); 170 171 mEnableAllButton.setEnabled(rotaryApplicationExists); 172 mDisableAllButton.setEnabled(rotaryApplicationExists); 173 mRotaryServiceToggle.setEnabled(rotaryApplicationExists); 174 mRotaryImeToggle.setEnabled(rotaryApplicationExists && rotaryImeExists); 175 mRotaryPlaygroundToggle.setEnabled(rotaryApplicationExists && rotaryPlaygroundExists); 176 177 mRotaryServiceToggle.setChecked(isRotaryServiceEnabled(getContext())); 178 mRotaryImeToggle.setChecked( 179 rotaryImeExists && isApplicationEnabled(getContext(), ROTARY_IME_PACKAGE)); 180 mRotaryPlaygroundToggle.setChecked( 181 rotaryPlaygroundExists && isApplicationEnabled(getContext(), 182 ROTARY_PLAYGROUND_PACKAGE)); 183 } 184 185 @Override onStop()186 public void onStop() { 187 super.onStop(); 188 189 getContext().unregisterReceiver(mPackagesUpdatedReceiver); 190 } 191 192 @Nullable findApplication(Context context, String packageName)193 private static ApplicationInfo findApplication(Context context, String packageName) { 194 PackageManager pm = context.getPackageManager(); 195 try { 196 Log.d(TAG, "Searching for: " + packageName); 197 return pm.getApplicationInfoAsUser(packageName, /* flags= */ 0, 198 ActivityManager.getCurrentUser()); 199 } catch (PackageManager.NameNotFoundException e) { 200 Log.e(TAG, "Could not find: " + packageName); 201 return null; 202 } 203 } 204 isApplicationEnabled(Context context, String packageName)205 private static boolean isApplicationEnabled(Context context, String packageName) { 206 ApplicationInfo info = findApplication(context, packageName); 207 if (info == null) { 208 return false; 209 } 210 211 return info.enabled; 212 } 213 enableApplication(Context context, String packageName, boolean enable)214 private static void enableApplication(Context context, String packageName, boolean enable) { 215 // Check that the application exists. 216 ApplicationInfo info = findApplication(context, packageName); 217 if (info == null) { 218 Log.e(TAG, "Cannot enable application. " + packageName + " package does not exist"); 219 return; 220 } 221 222 PackageManager pm = context.getPackageManager(); 223 int currentState = pm.getApplicationEnabledSetting(packageName); 224 int desiredState = enable ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED 225 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED; 226 227 boolean isEnabled = currentState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; 228 if (isEnabled != enable 229 || currentState == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { 230 Log.d(TAG, "Application state updated for " + packageName + ": " + enable); 231 pm.setApplicationEnabledSetting(packageName, desiredState, /* flags= */ 0); 232 } 233 } 234 isRotaryServiceEnabled(Context context)235 private static boolean isRotaryServiceEnabled(Context context) { 236 ApplicationInfo info = findApplication(context, ROTARY_CONTROLLER_PACKAGE); 237 if (info == null) { 238 return false; 239 } 240 241 int isAccessibilityEnabled = Settings.Secure.getInt(context.getContentResolver(), 242 Settings.Secure.ACCESSIBILITY_ENABLED, OFF); 243 if (isAccessibilityEnabled != ON) { 244 return false; 245 } 246 247 String services = Settings.Secure.getString(context.getContentResolver(), 248 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); 249 if (services == null) { 250 return false; 251 } 252 253 return services.contains(ROTARY_CONTROLLER_PACKAGE); 254 } 255 enableRotaryService(Context context, boolean enable)256 private static void enableRotaryService(Context context, boolean enable) { 257 // Check that the RotaryController exists. 258 ApplicationInfo info = findApplication(context, ROTARY_CONTROLLER_PACKAGE); 259 if (info == null) { 260 Log.e(TAG, "Cannot enable rotary service. " + ROTARY_CONTROLLER_PACKAGE 261 + " package does not exist"); 262 return; 263 } 264 265 // Set the list of accessibility services. 266 String services = Settings.Secure.getString(context.getContentResolver(), 267 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES); 268 if (services == null) { 269 services = ""; 270 } 271 Log.d(TAG, "Current list of accessibility services: " + services); 272 273 String[] servicesArray = services.split(ACCESSIBILITY_DELIMITER); 274 Set<ComponentName> servicesSet = new HashSet<>(); 275 for (String service : servicesArray) { 276 ComponentName name = ComponentName.unflattenFromString(service); 277 if (name != null) { 278 servicesSet.add(name); 279 } 280 } 281 282 if (enable) { 283 servicesSet.add(ROTARY_SERVICE_NAME); 284 } else { 285 servicesSet.remove(ROTARY_SERVICE_NAME); 286 } 287 288 StringBuilder builder = new StringBuilder(); 289 for (ComponentName service : servicesSet) { 290 if (builder.length() > 0) { 291 builder.append(ACCESSIBILITY_DELIMITER); 292 } 293 builder.append(service.flattenToString()); 294 } 295 296 Log.d(TAG, "New list of accessibility services: " + builder); 297 Settings.Secure.putString(context.getContentResolver(), 298 Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES, 299 builder.toString()); 300 301 // Set the overall enabled state. 302 int desiredState = enable ? ON : OFF; 303 Settings.Secure.putInt(context.getContentResolver(), Settings.Secure.ACCESSIBILITY_ENABLED, 304 desiredState); 305 } 306 } 307