1 /*
2  * Copyright (C) 2016 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.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.Bundle;
26 import android.service.quicksettings.TileService;
27 import android.support.v14.preference.SwitchPreference;
28 import android.support.v7.preference.Preference;
29 
30 import com.android.settings.SettingsActivity;
31 import com.android.settings.SettingsPreferenceFragment;
32 import com.android.settings.core.instrumentation.Instrumentable;
33 
34 public class DevelopmentTileConfigActivity extends SettingsActivity {
35 
36     @Override
getIntent()37     public Intent getIntent() {
38         Intent modIntent = new Intent(super.getIntent())
39                 .putExtra(EXTRA_SHOW_FRAGMENT, DevelopmentTileConfigFragment.class.getName())
40                 .putExtra(EXTRA_HIDE_DRAWER, true);
41         return modIntent;
42     }
43 
44     @Override
isValidFragment(String fragmentName)45     protected boolean isValidFragment(String fragmentName) {
46         return (DevelopmentTileConfigFragment.class.getName().equals(fragmentName));
47     }
48 
49     public static class DevelopmentTileConfigFragment extends SettingsPreferenceFragment
50             implements Preference.OnPreferenceChangeListener {
51 
52         @Override
onCreate(Bundle icicle)53         public void onCreate(Bundle icicle) {
54             super.onCreate(icicle);
55 
56             Context context = getPrefContext();
57             setPreferenceScreen(getPreferenceManager().createPreferenceScreen(context));
58             getPreferenceScreen().removeAll();
59 
60             Intent intent = new Intent(TileService.ACTION_QS_TILE)
61                     .setPackage(context.getPackageName());
62             PackageManager pm = getPackageManager();
63             for (ResolveInfo info :
64                     pm.queryIntentServices(intent, PackageManager.MATCH_DISABLED_COMPONENTS)) {
65                 ServiceInfo sInfo = info.serviceInfo;
66                 int enabledSetting = pm.getComponentEnabledSetting(
67                         new ComponentName(sInfo.packageName, sInfo.name));
68                 boolean checked = enabledSetting == PackageManager.COMPONENT_ENABLED_STATE_ENABLED
69                         || ((enabledSetting == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT)
70                         && sInfo.enabled);
71 
72                 SwitchPreference preference = new SwitchPreference(context);
73                 preference.setTitle(sInfo.loadLabel(pm));
74                 preference.setIcon(sInfo.icon);
75                 preference.setKey(sInfo.name);
76                 preference.setChecked(checked);
77                 preference.setPersistent(false);
78                 preference.setOnPreferenceChangeListener(this);
79                 getPreferenceScreen().addPreference(preference);
80             }
81         }
82 
83         @Override
getMetricsCategory()84         public int getMetricsCategory() {
85             return Instrumentable.METRICS_CATEGORY_UNKNOWN;
86         }
87 
88         @Override
onPreferenceChange(Preference preference, Object newValue)89         public boolean onPreferenceChange(Preference preference, Object newValue) {
90             ComponentName cn = new ComponentName(
91                     getPrefContext().getPackageName(), preference.getKey());
92             getPackageManager().setComponentEnabledSetting(cn, (Boolean) newValue
93                             ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED
94                             : PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
95                     PackageManager.DONT_KILL_APP);
96             return true;
97         }
98     }
99 }