Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 1 | /** |
| 2 | * Copyright (C) 2015 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 | * |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 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 | package com.android.settingslib.drawer; |
| 17 | |
| 18 | import android.annotation.LayoutRes; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.app.Activity; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 21 | import android.content.BroadcastReceiver; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 22 | import android.content.ComponentName; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.IntentFilter; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 26 | import android.content.pm.PackageManager; |
Jason Monk | f18a55d | 2015-11-12 10:52:21 -0500 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 28 | import android.os.AsyncTask; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 29 | import android.os.Bundle; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 30 | import android.util.ArraySet; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 31 | import android.util.Log; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 33 | import android.view.View; |
| 34 | import android.view.ViewGroup; |
| 35 | import android.view.Window; |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 36 | import android.view.WindowManager.LayoutParams; |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 37 | import android.widget.FrameLayout; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 38 | import android.widget.Toolbar; |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 39 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 40 | import com.android.settingslib.R; |
| 41 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 42 | import java.util.ArrayList; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 43 | import java.util.List; |
| 44 | |
| 45 | public class SettingsDrawerActivity extends Activity { |
| 46 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 47 | protected static final boolean DEBUG_TIMING = false; |
| 48 | private static final String TAG = "SettingsDrawerActivity"; |
Fan Zhang | 089a2c0 | 2016-09-15 14:28:19 -0700 | [diff] [blame] | 49 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 50 | |
Clara Bayarri | 962694d | 2016-04-29 16:56:02 +0100 | [diff] [blame] | 51 | public static final String EXTRA_SHOW_MENU = "show_drawer_menu"; |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 52 | |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 53 | // Serves as a temporary list of tiles to ignore until we heard back from the PM that they |
| 54 | // are disabled. |
| 55 | private static ArraySet<ComponentName> sTileBlacklist = new ArraySet<>(); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 56 | |
| 57 | private final PackageReceiver mPackageReceiver = new PackageReceiver(); |
| 58 | private final List<CategoryListener> mCategoryListeners = new ArrayList<>(); |
| 59 | |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 60 | private FrameLayout mContentHeaderContainer; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 61 | |
| 62 | @Override |
| 63 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 64 | super.onCreate(savedInstanceState); |
| 65 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 66 | long startTime = System.currentTimeMillis(); |
| 67 | |
Udam Saini | 48987f6 | 2016-03-03 13:20:18 -0800 | [diff] [blame] | 68 | TypedArray theme = getTheme().obtainStyledAttributes(android.R.styleable.Theme); |
| 69 | if (!theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) { |
| 70 | getWindow().addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
Udam Saini | 48987f6 | 2016-03-03 13:20:18 -0800 | [diff] [blame] | 71 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 72 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 73 | super.setContentView(R.layout.settings_with_drawer); |
Fan Zhang | 92a2613 | 2018-02-06 14:24:18 -0800 | [diff] [blame] | 74 | mContentHeaderContainer = findViewById(R.id.content_header_container); |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 75 | |
Fan Zhang | 92a2613 | 2018-02-06 14:24:18 -0800 | [diff] [blame] | 76 | Toolbar toolbar = findViewById(R.id.action_bar); |
Jason Monk | f18a55d | 2015-11-12 10:52:21 -0500 | [diff] [blame] | 77 | if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) { |
| 78 | toolbar.setVisibility(View.GONE); |
Jason Monk | f18a55d | 2015-11-12 10:52:21 -0500 | [diff] [blame] | 79 | return; |
| 80 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 81 | setActionBar(toolbar); |
jackqdyulei | dfc6ce5 | 2016-08-24 09:48:54 -0700 | [diff] [blame] | 82 | |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 83 | if (DEBUG_TIMING) { |
| 84 | Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime) |
| 85 | + " ms"); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 86 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | @Override |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame] | 90 | public boolean onNavigateUp() { |
Fan Zhang | 92a2613 | 2018-02-06 14:24:18 -0800 | [diff] [blame] | 91 | if (!super.onNavigateUp()) { |
| 92 | finish(); |
| 93 | } |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 94 | return true; |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | @Override |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 98 | protected void onResume() { |
| 99 | super.onResume(); |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 100 | final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 101 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 102 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 103 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 104 | filter.addDataScheme("package"); |
| 105 | registerReceiver(mPackageReceiver, filter); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 106 | |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 107 | new CategoriesUpdateTask().execute(); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | @Override |
| 111 | protected void onPause() { |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 112 | unregisterReceiver(mPackageReceiver); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 113 | super.onPause(); |
| 114 | } |
| 115 | |
| 116 | public void addCategoryListener(CategoryListener listener) { |
| 117 | mCategoryListeners.add(listener); |
| 118 | } |
| 119 | |
| 120 | public void remCategoryListener(CategoryListener listener) { |
| 121 | mCategoryListeners.remove(listener); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 122 | } |
| 123 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 124 | @Override |
| 125 | public void setContentView(@LayoutRes int layoutResID) { |
Fan Zhang | 34106dd | 2017-04-21 10:06:21 -0700 | [diff] [blame] | 126 | final ViewGroup parent = findViewById(R.id.content_frame); |
Jason Monk | 4ea6657 | 2016-03-30 11:05:51 -0400 | [diff] [blame] | 127 | if (parent != null) { |
| 128 | parent.removeAllViews(); |
| 129 | } |
| 130 | LayoutInflater.from(this).inflate(layoutResID, parent); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public void setContentView(View view) { |
| 135 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 140 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params); |
| 141 | } |
| 142 | |
Fan Zhang | 34106dd | 2017-04-21 10:06:21 -0700 | [diff] [blame] | 143 | private void onCategoriesChanged() { |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 144 | final int N = mCategoryListeners.size(); |
| 145 | for (int i = 0; i < N; i++) { |
| 146 | mCategoryListeners.get(i).onCategoriesChanged(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 147 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 148 | } |
| 149 | |
Fan Zhang | f5e032b | 2017-07-05 15:24:03 -0700 | [diff] [blame] | 150 | /** |
| 151 | * @return whether or not the enabled state actually changed. |
| 152 | */ |
| 153 | public boolean setTileEnabled(ComponentName component, boolean enabled) { |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 154 | PackageManager pm = getPackageManager(); |
| 155 | int state = pm.getComponentEnabledSetting(component); |
| 156 | boolean isEnabled = state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
| 157 | if (isEnabled != enabled || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 158 | if (enabled) { |
| 159 | sTileBlacklist.remove(component); |
| 160 | } else { |
| 161 | sTileBlacklist.add(component); |
| 162 | } |
| 163 | pm.setComponentEnabledSetting(component, enabled |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 164 | ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 165 | : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 166 | PackageManager.DONT_KILL_APP); |
Fan Zhang | f5e032b | 2017-07-05 15:24:03 -0700 | [diff] [blame] | 167 | return true; |
Fan Zhang | d2d3e2cc | 2017-01-06 11:44:38 -0800 | [diff] [blame] | 168 | } |
Fan Zhang | f5e032b | 2017-07-05 15:24:03 -0700 | [diff] [blame] | 169 | return false; |
Fan Zhang | d2d3e2cc | 2017-01-06 11:44:38 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Updates dashboard categories. Only necessary to call this after setTileEnabled |
| 174 | */ |
| 175 | public void updateCategories() { |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 176 | new CategoriesUpdateTask().execute(); |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 177 | } |
| 178 | |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 179 | public String getSettingPkg() { |
| 180 | return TileUtils.SETTING_PKG; |
| 181 | } |
| 182 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 183 | public interface CategoryListener { |
| 184 | void onCategoriesChanged(); |
| 185 | } |
| 186 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 187 | private class CategoriesUpdateTask extends AsyncTask<Void, Void, Void> { |
| 188 | |
| 189 | private final CategoryManager mCategoryManager; |
| 190 | |
| 191 | public CategoriesUpdateTask() { |
Fan Zhang | 5adc266 | 2016-10-04 15:12:58 -0700 | [diff] [blame] | 192 | mCategoryManager = CategoryManager.get(SettingsDrawerActivity.this); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | @Override |
| 196 | protected Void doInBackground(Void... params) { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame] | 197 | mCategoryManager.reloadAllCategories(SettingsDrawerActivity.this, getSettingPkg()); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 198 | return null; |
| 199 | } |
| 200 | |
| 201 | @Override |
| 202 | protected void onPostExecute(Void result) { |
| 203 | mCategoryManager.updateCategoryFromBlacklist(sTileBlacklist); |
| 204 | onCategoriesChanged(); |
| 205 | } |
| 206 | } |
| 207 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 208 | private class PackageReceiver extends BroadcastReceiver { |
| 209 | @Override |
| 210 | public void onReceive(Context context, Intent intent) { |
Fan Zhang | f1c1bb4 | 2017-02-27 16:04:32 -0800 | [diff] [blame] | 211 | new CategoriesUpdateTask().execute(); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 212 | } |
| 213 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 214 | } |