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 | * |
| 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 | package com.android.settingslib.drawer; |
| 17 | |
| 18 | import android.annotation.LayoutRes; |
| 19 | import android.annotation.Nullable; |
| 20 | import android.app.Activity; |
Jason Monk | 6bea950 | 2016-02-25 13:07:41 -0500 | [diff] [blame] | 21 | import android.content.ActivityNotFoundException; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 23 | import android.content.ComponentName; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.content.IntentFilter; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 27 | import android.content.pm.PackageManager; |
Jason Monk | f18a55d | 2015-11-12 10:52:21 -0500 | [diff] [blame] | 28 | import android.content.res.TypedArray; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 29 | import android.os.AsyncTask; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 30 | import android.os.Bundle; |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 31 | import android.provider.Settings; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 32 | import android.support.v4.widget.DrawerLayout; |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 33 | import android.util.ArraySet; |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 34 | import android.util.Log; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 35 | import android.util.Pair; |
| 36 | import android.view.Gravity; |
| 37 | import android.view.LayoutInflater; |
| 38 | import android.view.MenuItem; |
| 39 | import android.view.View; |
| 40 | import android.view.ViewGroup; |
| 41 | import android.view.Window; |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 42 | import android.view.WindowManager.LayoutParams; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 43 | import android.widget.AdapterView; |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 44 | import android.widget.FrameLayout; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 45 | import android.widget.ListView; |
| 46 | import android.widget.Toolbar; |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 47 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 48 | import com.android.settingslib.R; |
Jason Monk | ec02e47 | 2016-03-28 10:44:43 -0400 | [diff] [blame] | 49 | import com.android.settingslib.applications.InterestingConfigChanges; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 50 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 51 | import java.util.ArrayList; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 52 | import java.util.HashMap; |
| 53 | import java.util.List; |
| 54 | |
| 55 | public class SettingsDrawerActivity extends Activity { |
| 56 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 57 | protected static final boolean DEBUG_TIMING = false; |
| 58 | private static final String TAG = "SettingsDrawerActivity"; |
Fan Zhang | 089a2c0 | 2016-09-15 14:28:19 -0700 | [diff] [blame] | 59 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 60 | |
Clara Bayarri | 962694d | 2016-04-29 16:56:02 +0100 | [diff] [blame] | 61 | public static final String EXTRA_SHOW_MENU = "show_drawer_menu"; |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 62 | |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 63 | // Serves as a temporary list of tiles to ignore until we heard back from the PM that they |
| 64 | // are disabled. |
| 65 | private static ArraySet<ComponentName> sTileBlacklist = new ArraySet<>(); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 66 | |
| 67 | private final PackageReceiver mPackageReceiver = new PackageReceiver(); |
| 68 | private final List<CategoryListener> mCategoryListeners = new ArrayList<>(); |
| 69 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 70 | private SettingsDrawerAdapter mDrawerAdapter; |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 71 | private FrameLayout mContentHeaderContainer; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 72 | private DrawerLayout mDrawerLayout; |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 73 | private boolean mShowingMenu; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 74 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 75 | // Remove below after new IA |
| 76 | @Deprecated |
| 77 | private static List<DashboardCategory> sDashboardCategories; |
| 78 | @Deprecated |
| 79 | private static HashMap<Pair<String, String>, Tile> sTileCache; |
| 80 | @Deprecated |
| 81 | private static InterestingConfigChanges sConfigTracker; |
| 82 | // Remove above after new IA |
| 83 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 84 | @Override |
| 85 | protected void onCreate(@Nullable Bundle savedInstanceState) { |
| 86 | super.onCreate(savedInstanceState); |
| 87 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 88 | long startTime = System.currentTimeMillis(); |
| 89 | |
Udam Saini | 48987f6 | 2016-03-03 13:20:18 -0800 | [diff] [blame] | 90 | TypedArray theme = getTheme().obtainStyledAttributes(android.R.styleable.Theme); |
| 91 | if (!theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) { |
| 92 | getWindow().addFlags(LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
| 93 | getWindow().addFlags(LayoutParams.FLAG_TRANSLUCENT_STATUS); |
| 94 | requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 95 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 96 | super.setContentView(R.layout.settings_with_drawer); |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 97 | mContentHeaderContainer = (FrameLayout) findViewById(R.id.content_header_container); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 98 | mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 99 | if (mDrawerLayout == null) { |
| 100 | return; |
| 101 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 102 | Toolbar toolbar = (Toolbar) findViewById(R.id.action_bar); |
Jason Monk | f18a55d | 2015-11-12 10:52:21 -0500 | [diff] [blame] | 103 | if (theme.getBoolean(android.R.styleable.Theme_windowNoTitle, false)) { |
| 104 | toolbar.setVisibility(View.GONE); |
| 105 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 106 | mDrawerLayout = null; |
| 107 | return; |
| 108 | } |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame] | 109 | if (!isNavDrawerEnabled()) { |
| 110 | setIsDrawerPresent(false); |
| 111 | } |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 112 | if (!isDashboardFeatureEnabled()) { |
| 113 | getDashboardCategories(); |
| 114 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 115 | setActionBar(toolbar); |
| 116 | mDrawerAdapter = new SettingsDrawerAdapter(this); |
| 117 | ListView listView = (ListView) findViewById(R.id.left_drawer); |
| 118 | listView.setAdapter(mDrawerAdapter); |
| 119 | listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { |
| 120 | public void onItemClick(android.widget.AdapterView<?> parent, View view, int position, |
| 121 | long id) { |
| 122 | onTileClicked(mDrawerAdapter.getTile(position)); |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 123 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 124 | }); |
jackqdyulei | dfc6ce5 | 2016-08-24 09:48:54 -0700 | [diff] [blame] | 125 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 126 | if (DEBUG_TIMING) Log.d(TAG, "onCreate took " + (System.currentTimeMillis() - startTime) |
| 127 | + " ms"); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public boolean onOptionsItemSelected(MenuItem item) { |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 132 | if (mShowingMenu && mDrawerLayout != null && item.getItemId() == android.R.id.home |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 133 | && mDrawerAdapter.getCount() != 0) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 134 | openDrawer(); |
| 135 | return true; |
| 136 | } |
| 137 | return super.onOptionsItemSelected(item); |
| 138 | } |
| 139 | |
| 140 | @Override |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame] | 141 | public boolean onNavigateUp() { |
| 142 | if (!isNavDrawerEnabled()) { |
| 143 | finish(); |
| 144 | return true; |
| 145 | } |
| 146 | return super.onNavigateUp(); |
| 147 | } |
| 148 | |
| 149 | @Override |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 150 | protected void onResume() { |
| 151 | super.onResume(); |
| 152 | |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 153 | if (mDrawerLayout != null) { |
| 154 | final IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED); |
| 155 | filter.addAction(Intent.ACTION_PACKAGE_REMOVED); |
| 156 | filter.addAction(Intent.ACTION_PACKAGE_CHANGED); |
| 157 | filter.addAction(Intent.ACTION_PACKAGE_REPLACED); |
| 158 | filter.addDataScheme("package"); |
| 159 | registerReceiver(mPackageReceiver, filter); |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 160 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 161 | if (isDashboardFeatureEnabled()) { |
| 162 | new CategoriesUpdateTask().execute(); |
| 163 | } else { |
| 164 | new CategoriesUpdater().execute(); |
| 165 | } |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 166 | } |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 167 | final Intent intent = getIntent(); |
| 168 | if (intent != null) { |
| 169 | if (intent.hasExtra(EXTRA_SHOW_MENU)) { |
| 170 | if (intent.getBooleanExtra(EXTRA_SHOW_MENU, false)) { |
| 171 | // Intent explicitly set to show menu. |
| 172 | showMenuIcon(); |
| 173 | } |
| 174 | } else if (isTopLevelTile(intent)) { |
| 175 | showMenuIcon(); |
| 176 | } |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 177 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Override |
| 181 | protected void onPause() { |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 182 | if (mDrawerLayout != null) { |
| 183 | unregisterReceiver(mPackageReceiver); |
| 184 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 185 | |
| 186 | super.onPause(); |
| 187 | } |
| 188 | |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 189 | private boolean isTopLevelTile(Intent intent) { |
| 190 | final ComponentName componentName = intent.getComponent(); |
| 191 | if (componentName == null) { |
| 192 | return false; |
| 193 | } |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 194 | if (isDashboardFeatureEnabled()) { |
Fan Zhang | 5adc266 | 2016-10-04 15:12:58 -0700 | [diff] [blame] | 195 | final DashboardCategory homepageCategories = CategoryManager.get(this) |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame^] | 196 | .getTilesByCategory(this, CategoryKey.CATEGORY_HOMEPAGE, getSettingPkg()); |
| 197 | return homepageCategories == |
| 198 | null ? false : homepageCategories.containsComponent(componentName); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 199 | } else { |
| 200 | // Look for a tile that has the same component as incoming intent |
| 201 | final List<DashboardCategory> categories = getDashboardCategories(); |
| 202 | for (DashboardCategory category : categories) { |
| 203 | if (category.containsComponent(componentName)) { |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 204 | return true; |
| 205 | } |
| 206 | } |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 207 | if (DEBUG) { |
| 208 | Log.d(TAG, "Intent is not for top level settings " + intent); |
| 209 | } |
| 210 | return false; |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 211 | } |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 212 | } |
| 213 | |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame^] | 214 | /** |
| 215 | * Gets the name of the intent action of the default setting app. Used to launch setting app |
| 216 | * when Settings Home is clicked. |
| 217 | */ |
| 218 | public String getSettingAction() { |
| 219 | return Settings.ACTION_SETTINGS; |
| 220 | } |
| 221 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 222 | public void addCategoryListener(CategoryListener listener) { |
| 223 | mCategoryListeners.add(listener); |
| 224 | } |
| 225 | |
| 226 | public void remCategoryListener(CategoryListener listener) { |
| 227 | mCategoryListeners.remove(listener); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 228 | } |
| 229 | |
Anna Galusza | 1774a27 | 2016-02-01 15:44:00 -0800 | [diff] [blame] | 230 | public void setIsDrawerPresent(boolean isPresent) { |
Anna Galusza | fb695c6 | 2016-02-02 10:06:05 -0800 | [diff] [blame] | 231 | if (isPresent) { |
Anna Galusza | 1774a27 | 2016-02-01 15:44:00 -0800 | [diff] [blame] | 232 | mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 233 | updateDrawer(); |
| 234 | } else { |
Udam Saini | 68dab67 | 2016-03-29 10:49:18 -0700 | [diff] [blame] | 235 | if (mDrawerLayout != null) { |
| 236 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 237 | mDrawerLayout = null; |
| 238 | } |
Anna Galusza | 1774a27 | 2016-02-01 15:44:00 -0800 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 242 | public void openDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 243 | if (mDrawerLayout != null) { |
| 244 | mDrawerLayout.openDrawer(Gravity.START); |
| 245 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | public void closeDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 249 | if (mDrawerLayout != null) { |
| 250 | mDrawerLayout.closeDrawers(); |
| 251 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 252 | } |
| 253 | |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 254 | public void setContentHeaderView(View headerView) { |
| 255 | mContentHeaderContainer.removeAllViews(); |
| 256 | if (headerView != null) { |
| 257 | mContentHeaderContainer.addView(headerView); |
| 258 | } |
| 259 | } |
| 260 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 261 | @Override |
| 262 | public void setContentView(@LayoutRes int layoutResID) { |
Jason Monk | 4ea6657 | 2016-03-30 11:05:51 -0400 | [diff] [blame] | 263 | final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame); |
| 264 | if (parent != null) { |
| 265 | parent.removeAllViews(); |
| 266 | } |
| 267 | LayoutInflater.from(this).inflate(layoutResID, parent); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | @Override |
| 271 | public void setContentView(View view) { |
| 272 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view); |
| 273 | } |
| 274 | |
| 275 | @Override |
| 276 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 277 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params); |
| 278 | } |
| 279 | |
| 280 | public void updateDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 281 | if (mDrawerLayout == null) { |
| 282 | return; |
| 283 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 284 | // TODO: Do this in the background with some loading. |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 285 | if (isDashboardFeatureEnabled()) { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame^] | 286 | mDrawerAdapter.updateHomepageCategories(getSettingPkg()); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 287 | } else { |
| 288 | mDrawerAdapter.updateCategories(); |
| 289 | } |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 290 | if (mDrawerAdapter.getCount() != 0) { |
| 291 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 292 | } else { |
| 293 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 294 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 295 | } |
| 296 | |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 297 | public void showMenuIcon() { |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 298 | getActionBar().setDisplayHomeAsUpEnabled(true); |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame] | 299 | if (isNavDrawerEnabled()) { |
| 300 | mShowingMenu = true; |
| 301 | getActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); |
| 302 | getActionBar().setHomeActionContentDescription( |
| 303 | R.string.content_description_menu_button); |
| 304 | } |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 305 | } |
| 306 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 307 | public List<DashboardCategory> getDashboardCategories() { |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 308 | if (sDashboardCategories == null) { |
| 309 | sTileCache = new HashMap<>(); |
Jason Monk | ec02e47 | 2016-03-28 10:44:43 -0400 | [diff] [blame] | 310 | sConfigTracker = new InterestingConfigChanges(); |
Jason Monk | b0ae8f8 | 2016-04-12 10:30:43 -0400 | [diff] [blame] | 311 | // Apply initial current config. |
| 312 | sConfigTracker.applyNewConfig(getResources()); |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 313 | sDashboardCategories = TileUtils.getCategories(this, sTileCache); |
| 314 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 315 | return sDashboardCategories; |
| 316 | } |
| 317 | |
| 318 | protected void onCategoriesChanged() { |
| 319 | updateDrawer(); |
| 320 | final int N = mCategoryListeners.size(); |
| 321 | for (int i = 0; i < N; i++) { |
| 322 | mCategoryListeners.get(i).onCategoriesChanged(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 323 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 324 | } |
| 325 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 326 | public boolean openTile(Tile tile) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 327 | closeDrawer(); |
Jason Monk | 3175a6e | 2015-11-30 15:05:35 -0500 | [diff] [blame] | 328 | if (tile == null) { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame^] | 329 | Intent intent = new Intent(getSettingAction()).addFlags( |
| 330 | Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 331 | startActivity(intent); |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 332 | return true; |
Jason Monk | 3175a6e | 2015-11-30 15:05:35 -0500 | [diff] [blame] | 333 | } |
Jason Monk | 6bea950 | 2016-02-25 13:07:41 -0500 | [diff] [blame] | 334 | try { |
Fan Zhang | 11b6507 | 2016-10-31 16:45:00 -0700 | [diff] [blame] | 335 | ProfileSelectDialog.updateUserHandlesIfNeeded(this /* context */, tile); |
Jason Monk | 6bea950 | 2016-02-25 13:07:41 -0500 | [diff] [blame] | 336 | int numUserHandles = tile.userHandle.size(); |
| 337 | if (numUserHandles > 1) { |
| 338 | ProfileSelectDialog.show(getFragmentManager(), tile); |
| 339 | return false; |
| 340 | } else if (numUserHandles == 1) { |
| 341 | // Show menu on top level items. |
| 342 | tile.intent.putExtra(EXTRA_SHOW_MENU, true); |
| 343 | tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 344 | startActivityAsUser(tile.intent, tile.userHandle.get(0)); |
| 345 | } else { |
| 346 | // Show menu on top level items. |
| 347 | tile.intent.putExtra(EXTRA_SHOW_MENU, true); |
| 348 | tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 349 | startActivity(tile.intent); |
| 350 | } |
| 351 | } catch (ActivityNotFoundException e) { |
| 352 | Log.w(TAG, "Couldn't find tile " + tile.intent, e); |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 353 | } |
| 354 | return true; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 355 | } |
| 356 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 357 | protected void onTileClicked(Tile tile) { |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 358 | if (openTile(tile)) { |
| 359 | finish(); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | public void onProfileTileOpen() { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 364 | finish(); |
| 365 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 366 | |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 367 | public void setTileEnabled(ComponentName component, boolean enabled) { |
| 368 | PackageManager pm = getPackageManager(); |
| 369 | int state = pm.getComponentEnabledSetting(component); |
| 370 | boolean isEnabled = state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
| 371 | if (isEnabled != enabled || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 372 | if (enabled) { |
| 373 | sTileBlacklist.remove(component); |
| 374 | } else { |
| 375 | sTileBlacklist.add(component); |
| 376 | } |
| 377 | pm.setComponentEnabledSetting(component, enabled |
| 378 | ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 379 | : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 380 | PackageManager.DONT_KILL_APP); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 381 | if (isDashboardFeatureEnabled()) { |
| 382 | new CategoriesUpdateTask().execute(); |
| 383 | } else { |
| 384 | new CategoriesUpdater().execute(); |
| 385 | } |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 386 | } |
| 387 | } |
| 388 | |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame^] | 389 | public String getSettingPkg() { |
| 390 | return TileUtils.SETTING_PKG; |
| 391 | } |
| 392 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 393 | public interface CategoryListener { |
| 394 | void onCategoriesChanged(); |
| 395 | } |
| 396 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 397 | /** |
| 398 | * @deprecated remove after new IA |
| 399 | */ |
| 400 | @Deprecated |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 401 | private class CategoriesUpdater extends AsyncTask<Void, Void, List<DashboardCategory>> { |
| 402 | @Override |
| 403 | protected List<DashboardCategory> doInBackground(Void... params) { |
Jason Monk | ec02e47 | 2016-03-28 10:44:43 -0400 | [diff] [blame] | 404 | if (sConfigTracker.applyNewConfig(getResources())) { |
| 405 | sTileCache.clear(); |
| 406 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 407 | return TileUtils.getCategories(SettingsDrawerActivity.this, sTileCache); |
| 408 | } |
| 409 | |
| 410 | @Override |
Richard Ho | 8c4e4b0 | 2016-06-13 16:54:04 -0700 | [diff] [blame] | 411 | protected void onPreExecute() { |
| 412 | if (sConfigTracker == null || sTileCache == null) { |
| 413 | getDashboardCategories(); |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | @Override |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 418 | protected void onPostExecute(List<DashboardCategory> dashboardCategories) { |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 419 | for (int i = 0; i < dashboardCategories.size(); i++) { |
| 420 | DashboardCategory category = dashboardCategories.get(i); |
| 421 | for (int j = 0; j < category.tiles.size(); j++) { |
| 422 | Tile tile = category.tiles.get(j); |
| 423 | if (sTileBlacklist.contains(tile.intent.getComponent())) { |
| 424 | category.tiles.remove(j--); |
| 425 | } |
| 426 | } |
| 427 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 428 | sDashboardCategories = dashboardCategories; |
| 429 | onCategoriesChanged(); |
| 430 | } |
| 431 | } |
| 432 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 433 | private class CategoriesUpdateTask extends AsyncTask<Void, Void, Void> { |
| 434 | |
| 435 | private final CategoryManager mCategoryManager; |
| 436 | |
| 437 | public CategoriesUpdateTask() { |
Fan Zhang | 5adc266 | 2016-10-04 15:12:58 -0700 | [diff] [blame] | 438 | mCategoryManager = CategoryManager.get(SettingsDrawerActivity.this); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | @Override |
| 442 | protected Void doInBackground(Void... params) { |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame^] | 443 | mCategoryManager.reloadAllCategories(SettingsDrawerActivity.this, getSettingPkg()); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 444 | return null; |
| 445 | } |
| 446 | |
| 447 | @Override |
| 448 | protected void onPostExecute(Void result) { |
| 449 | mCategoryManager.updateCategoryFromBlacklist(sTileBlacklist); |
| 450 | onCategoriesChanged(); |
| 451 | } |
| 452 | } |
| 453 | |
roger xue | 8f06ab0 | 2016-12-08 14:09:50 -0800 | [diff] [blame^] | 454 | /** |
| 455 | * @return {@code true} if IA (Information Architecture) is enabled. |
| 456 | */ |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 457 | protected boolean isDashboardFeatureEnabled() { |
| 458 | return false; |
| 459 | } |
| 460 | |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame] | 461 | boolean isNavDrawerEnabled() { |
| 462 | return !isDashboardFeatureEnabled() |
| 463 | || getResources().getBoolean(R.bool.config_enable_nav_drawer); |
| 464 | } |
| 465 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 466 | private class PackageReceiver extends BroadcastReceiver { |
| 467 | @Override |
| 468 | public void onReceive(Context context, Intent intent) { |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 469 | if (isDashboardFeatureEnabled()) { |
| 470 | new CategoriesUpdateTask().execute(); |
| 471 | } else { |
| 472 | new CategoriesUpdater().execute(); |
| 473 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 474 | } |
| 475 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 476 | } |