1 /* 2 * Copyright (C) 2017 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.settings.development.qstile; 18 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.PackageManager; 23 import android.content.pm.ResolveInfo; 24 import android.content.pm.ServiceInfo; 25 import android.os.RemoteException; 26 import android.os.ServiceManager; 27 import android.os.SystemProperties; 28 import android.service.quicksettings.TileService; 29 import android.util.Log; 30 31 import androidx.annotation.VisibleForTesting; 32 import androidx.preference.Preference; 33 import androidx.preference.PreferenceScreen; 34 import androidx.preference.SwitchPreferenceCompat; 35 import androidx.preference.TwoStatePreference; 36 37 import com.android.internal.statusbar.IStatusBarService; 38 import com.android.settings.core.BasePreferenceController; 39 40 import java.util.ArrayList; 41 import java.util.List; 42 43 public class DevelopmentTilePreferenceController extends BasePreferenceController { 44 45 private static final String TAG = "DevTilePrefController"; 46 private final OnChangeHandler mOnChangeHandler; 47 private final PackageManager mPackageManager; 48 DevelopmentTilePreferenceController(Context context, String key)49 public DevelopmentTilePreferenceController(Context context, String key) { 50 super(context, key); 51 mOnChangeHandler = new OnChangeHandler(context); 52 mPackageManager = context.getPackageManager(); 53 } 54 55 @Override getAvailabilityStatus()56 public int getAvailabilityStatus() { 57 return AVAILABLE; 58 } 59 60 @Override displayPreference(PreferenceScreen screen)61 public void displayPreference(PreferenceScreen screen) { 62 super.displayPreference(screen); 63 final Context context = screen.getContext(); 64 List<ServiceInfo> serviceInfos = getTileServiceList(context); 65 66 for (ServiceInfo sInfo : serviceInfos) { 67 // Check if the tile requires a flag. If it does, hide tile if flag is off. 68 if (sInfo.metaData != null) { 69 String flag = sInfo.metaData.getString( 70 DevelopmentTiles.META_DATA_REQUIRES_SYSTEM_PROPERTY); 71 if (flag != null) { 72 boolean enabled = SystemProperties.getBoolean(flag, false); 73 if (!enabled) { 74 // Flagged tile, flag is not enabled 75 continue; 76 } 77 } 78 } 79 80 final int enabledSetting = mPackageManager.getComponentEnabledSetting( 81 new ComponentName(sInfo.packageName, sInfo.name)); 82 boolean checked = enabledSetting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED 83 || ((enabledSetting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) 84 && sInfo.enabled); 85 86 TwoStatePreference preference = new SwitchPreferenceCompat(context); 87 preference.setTitle(sInfo.loadLabel(mPackageManager)); 88 preference.setIcon(sInfo.icon); 89 preference.setKey(sInfo.name); 90 preference.setChecked(checked); 91 preference.setOnPreferenceChangeListener(mOnChangeHandler); 92 screen.addPreference(preference); 93 } 94 } 95 96 /** 97 * Get Quick Settings services from PackageManager 98 */ getTileServiceList(Context context)99 public static List<ServiceInfo> getTileServiceList(Context context) { 100 Intent intent = new Intent(TileService.ACTION_QS_TILE) 101 .setPackage(context.getPackageName()); 102 PackageManager packageManager = context.getPackageManager(); 103 List<ResolveInfo> resolveInfos = packageManager.queryIntentServices(intent, 104 PackageManager.MATCH_DISABLED_COMPONENTS | PackageManager.GET_META_DATA); 105 106 List<ServiceInfo> servicesInfos = new ArrayList<>(); 107 for (ResolveInfo info : resolveInfos) { 108 ServiceInfo sInfo = info.serviceInfo; 109 servicesInfos.add(sInfo); 110 } 111 return servicesInfos; 112 } 113 114 @VisibleForTesting 115 static class OnChangeHandler implements Preference.OnPreferenceChangeListener { 116 117 private final Context mContext; 118 private final PackageManager mPackageManager; 119 private IStatusBarService mStatusBarService; 120 OnChangeHandler(Context context)121 public OnChangeHandler(Context context) { 122 mContext = context; 123 mPackageManager = context.getPackageManager(); 124 mStatusBarService = IStatusBarService.Stub.asInterface( 125 ServiceManager.checkService(Context.STATUS_BAR_SERVICE)); 126 } 127 128 @Override onPreferenceChange(Preference preference, Object newValue)129 public boolean onPreferenceChange(Preference preference, Object newValue) { 130 boolean enabled = ((Boolean) newValue).booleanValue(); 131 ComponentName componentName = new ComponentName( 132 mContext.getPackageName(), preference.getKey()); 133 mPackageManager.setComponentEnabledSetting(componentName, enabled 134 ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED 135 : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 136 PackageManager.DONT_KILL_APP); 137 138 try { 139 if (mStatusBarService != null) { 140 if (enabled) { 141 mStatusBarService.addTile(componentName); 142 } else { 143 mStatusBarService.remTile(componentName); 144 } 145 } 146 } catch (RemoteException e) { 147 Log.e(TAG, "Failed to modify QS tile for component " + 148 componentName.toString(), e); 149 } 150 return true; 151 } 152 } 153 } 154