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) |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 196 | .getTilesByCategory(this, CategoryKey.CATEGORY_HOMEPAGE); |
| 197 | return homepageCategories.containsComponent(componentName); |
| 198 | } else { |
| 199 | // Look for a tile that has the same component as incoming intent |
| 200 | final List<DashboardCategory> categories = getDashboardCategories(); |
| 201 | for (DashboardCategory category : categories) { |
| 202 | if (category.containsComponent(componentName)) { |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 203 | return true; |
| 204 | } |
| 205 | } |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 206 | if (DEBUG) { |
| 207 | Log.d(TAG, "Intent is not for top level settings " + intent); |
| 208 | } |
| 209 | return false; |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 210 | } |
Fan Zhang | f311b74 | 2016-09-16 08:46:09 -0700 | [diff] [blame] | 211 | } |
| 212 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 213 | public void addCategoryListener(CategoryListener listener) { |
| 214 | mCategoryListeners.add(listener); |
| 215 | } |
| 216 | |
| 217 | public void remCategoryListener(CategoryListener listener) { |
| 218 | mCategoryListeners.remove(listener); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Anna Galusza | 1774a27 | 2016-02-01 15:44:00 -0800 | [diff] [blame] | 221 | public void setIsDrawerPresent(boolean isPresent) { |
Anna Galusza | fb695c6 | 2016-02-02 10:06:05 -0800 | [diff] [blame] | 222 | if (isPresent) { |
Anna Galusza | 1774a27 | 2016-02-01 15:44:00 -0800 | [diff] [blame] | 223 | mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); |
| 224 | updateDrawer(); |
| 225 | } else { |
Udam Saini | 68dab67 | 2016-03-29 10:49:18 -0700 | [diff] [blame] | 226 | if (mDrawerLayout != null) { |
| 227 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 228 | mDrawerLayout = null; |
| 229 | } |
Anna Galusza | 1774a27 | 2016-02-01 15:44:00 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 233 | public void openDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 234 | if (mDrawerLayout != null) { |
| 235 | mDrawerLayout.openDrawer(Gravity.START); |
| 236 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | public void closeDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 240 | if (mDrawerLayout != null) { |
| 241 | mDrawerLayout.closeDrawers(); |
| 242 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 243 | } |
| 244 | |
Fan Zhang | 7163d77 | 2016-03-29 10:30:13 -0700 | [diff] [blame] | 245 | public void setContentHeaderView(View headerView) { |
| 246 | mContentHeaderContainer.removeAllViews(); |
| 247 | if (headerView != null) { |
| 248 | mContentHeaderContainer.addView(headerView); |
| 249 | } |
| 250 | } |
| 251 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 252 | @Override |
| 253 | public void setContentView(@LayoutRes int layoutResID) { |
Jason Monk | 4ea6657 | 2016-03-30 11:05:51 -0400 | [diff] [blame] | 254 | final ViewGroup parent = (ViewGroup) findViewById(R.id.content_frame); |
| 255 | if (parent != null) { |
| 256 | parent.removeAllViews(); |
| 257 | } |
| 258 | LayoutInflater.from(this).inflate(layoutResID, parent); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public void setContentView(View view) { |
| 263 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view); |
| 264 | } |
| 265 | |
| 266 | @Override |
| 267 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 268 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params); |
| 269 | } |
| 270 | |
| 271 | public void updateDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 272 | if (mDrawerLayout == null) { |
| 273 | return; |
| 274 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 275 | // TODO: Do this in the background with some loading. |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 276 | if (isDashboardFeatureEnabled()) { |
| 277 | mDrawerAdapter.updateHomepageCategories(); |
| 278 | } else { |
| 279 | mDrawerAdapter.updateCategories(); |
| 280 | } |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 281 | if (mDrawerAdapter.getCount() != 0) { |
| 282 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 283 | } else { |
| 284 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 285 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 286 | } |
| 287 | |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 288 | public void showMenuIcon() { |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 289 | getActionBar().setDisplayHomeAsUpEnabled(true); |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame^] | 290 | if (isNavDrawerEnabled()) { |
| 291 | mShowingMenu = true; |
| 292 | getActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); |
| 293 | getActionBar().setHomeActionContentDescription( |
| 294 | R.string.content_description_menu_button); |
| 295 | } |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 296 | } |
| 297 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 298 | public List<DashboardCategory> getDashboardCategories() { |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 299 | if (sDashboardCategories == null) { |
| 300 | sTileCache = new HashMap<>(); |
Jason Monk | ec02e47 | 2016-03-28 10:44:43 -0400 | [diff] [blame] | 301 | sConfigTracker = new InterestingConfigChanges(); |
Jason Monk | b0ae8f8 | 2016-04-12 10:30:43 -0400 | [diff] [blame] | 302 | // Apply initial current config. |
| 303 | sConfigTracker.applyNewConfig(getResources()); |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 304 | sDashboardCategories = TileUtils.getCategories(this, sTileCache); |
| 305 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 306 | return sDashboardCategories; |
| 307 | } |
| 308 | |
| 309 | protected void onCategoriesChanged() { |
| 310 | updateDrawer(); |
| 311 | final int N = mCategoryListeners.size(); |
| 312 | for (int i = 0; i < N; i++) { |
| 313 | mCategoryListeners.get(i).onCategoriesChanged(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 314 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 315 | } |
| 316 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 317 | public boolean openTile(Tile tile) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 318 | closeDrawer(); |
Jason Monk | 3175a6e | 2015-11-30 15:05:35 -0500 | [diff] [blame] | 319 | if (tile == null) { |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 320 | startActivity(new Intent(Settings.ACTION_SETTINGS).addFlags( |
| 321 | Intent.FLAG_ACTIVITY_CLEAR_TASK)); |
| 322 | return true; |
Jason Monk | 3175a6e | 2015-11-30 15:05:35 -0500 | [diff] [blame] | 323 | } |
Jason Monk | 6bea950 | 2016-02-25 13:07:41 -0500 | [diff] [blame] | 324 | try { |
Fan Zhang | 11b6507 | 2016-10-31 16:45:00 -0700 | [diff] [blame] | 325 | ProfileSelectDialog.updateUserHandlesIfNeeded(this /* context */, tile); |
Jason Monk | 6bea950 | 2016-02-25 13:07:41 -0500 | [diff] [blame] | 326 | int numUserHandles = tile.userHandle.size(); |
| 327 | if (numUserHandles > 1) { |
| 328 | ProfileSelectDialog.show(getFragmentManager(), tile); |
| 329 | return false; |
| 330 | } else if (numUserHandles == 1) { |
| 331 | // Show menu on top level items. |
| 332 | tile.intent.putExtra(EXTRA_SHOW_MENU, true); |
| 333 | tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 334 | startActivityAsUser(tile.intent, tile.userHandle.get(0)); |
| 335 | } else { |
| 336 | // Show menu on top level items. |
| 337 | tile.intent.putExtra(EXTRA_SHOW_MENU, true); |
| 338 | tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 339 | startActivity(tile.intent); |
| 340 | } |
| 341 | } catch (ActivityNotFoundException e) { |
| 342 | Log.w(TAG, "Couldn't find tile " + tile.intent, e); |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 343 | } |
| 344 | return true; |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 345 | } |
| 346 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 347 | protected void onTileClicked(Tile tile) { |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 348 | if (openTile(tile)) { |
| 349 | finish(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | public void onProfileTileOpen() { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 354 | finish(); |
| 355 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 356 | |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 357 | public void setTileEnabled(ComponentName component, boolean enabled) { |
| 358 | PackageManager pm = getPackageManager(); |
| 359 | int state = pm.getComponentEnabledSetting(component); |
| 360 | boolean isEnabled = state == PackageManager.COMPONENT_ENABLED_STATE_ENABLED; |
| 361 | if (isEnabled != enabled || state == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT) { |
| 362 | if (enabled) { |
| 363 | sTileBlacklist.remove(component); |
| 364 | } else { |
| 365 | sTileBlacklist.add(component); |
| 366 | } |
| 367 | pm.setComponentEnabledSetting(component, enabled |
| 368 | ? PackageManager.COMPONENT_ENABLED_STATE_ENABLED |
| 369 | : PackageManager.COMPONENT_ENABLED_STATE_DISABLED, |
| 370 | PackageManager.DONT_KILL_APP); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 371 | if (isDashboardFeatureEnabled()) { |
| 372 | new CategoriesUpdateTask().execute(); |
| 373 | } else { |
| 374 | new CategoriesUpdater().execute(); |
| 375 | } |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 376 | } |
| 377 | } |
| 378 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 379 | public interface CategoryListener { |
| 380 | void onCategoriesChanged(); |
| 381 | } |
| 382 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 383 | /** |
| 384 | * @deprecated remove after new IA |
| 385 | */ |
| 386 | @Deprecated |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 387 | private class CategoriesUpdater extends AsyncTask<Void, Void, List<DashboardCategory>> { |
| 388 | @Override |
| 389 | protected List<DashboardCategory> doInBackground(Void... params) { |
Jason Monk | ec02e47 | 2016-03-28 10:44:43 -0400 | [diff] [blame] | 390 | if (sConfigTracker.applyNewConfig(getResources())) { |
| 391 | sTileCache.clear(); |
| 392 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 393 | return TileUtils.getCategories(SettingsDrawerActivity.this, sTileCache); |
| 394 | } |
| 395 | |
| 396 | @Override |
Richard Ho | 8c4e4b0 | 2016-06-13 16:54:04 -0700 | [diff] [blame] | 397 | protected void onPreExecute() { |
| 398 | if (sConfigTracker == null || sTileCache == null) { |
| 399 | getDashboardCategories(); |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | @Override |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 404 | protected void onPostExecute(List<DashboardCategory> dashboardCategories) { |
Jason Monk | fcad09a | 2016-06-07 14:01:32 -0400 | [diff] [blame] | 405 | for (int i = 0; i < dashboardCategories.size(); i++) { |
| 406 | DashboardCategory category = dashboardCategories.get(i); |
| 407 | for (int j = 0; j < category.tiles.size(); j++) { |
| 408 | Tile tile = category.tiles.get(j); |
| 409 | if (sTileBlacklist.contains(tile.intent.getComponent())) { |
| 410 | category.tiles.remove(j--); |
| 411 | } |
| 412 | } |
| 413 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 414 | sDashboardCategories = dashboardCategories; |
| 415 | onCategoriesChanged(); |
| 416 | } |
| 417 | } |
| 418 | |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 419 | private class CategoriesUpdateTask extends AsyncTask<Void, Void, Void> { |
| 420 | |
| 421 | private final CategoryManager mCategoryManager; |
| 422 | |
| 423 | public CategoriesUpdateTask() { |
Fan Zhang | 5adc266 | 2016-10-04 15:12:58 -0700 | [diff] [blame] | 424 | mCategoryManager = CategoryManager.get(SettingsDrawerActivity.this); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | @Override |
| 428 | protected Void doInBackground(Void... params) { |
Fan Zhang | c8a5b79 | 2016-10-03 15:05:53 -0700 | [diff] [blame] | 429 | mCategoryManager.reloadAllCategories(SettingsDrawerActivity.this); |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 430 | return null; |
| 431 | } |
| 432 | |
| 433 | @Override |
| 434 | protected void onPostExecute(Void result) { |
| 435 | mCategoryManager.updateCategoryFromBlacklist(sTileBlacklist); |
| 436 | onCategoriesChanged(); |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | protected boolean isDashboardFeatureEnabled() { |
| 441 | return false; |
| 442 | } |
| 443 | |
Fan Zhang | cf439c0 | 2017-01-03 13:06:11 -0800 | [diff] [blame^] | 444 | boolean isNavDrawerEnabled() { |
| 445 | return !isDashboardFeatureEnabled() |
| 446 | || getResources().getBoolean(R.bool.config_enable_nav_drawer); |
| 447 | } |
| 448 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 449 | private class PackageReceiver extends BroadcastReceiver { |
| 450 | @Override |
| 451 | public void onReceive(Context context, Intent intent) { |
Fan Zhang | 22a56d7 | 2016-09-27 17:52:00 -0700 | [diff] [blame] | 452 | if (isDashboardFeatureEnabled()) { |
| 453 | new CategoriesUpdateTask().execute(); |
| 454 | } else { |
| 455 | new CategoriesUpdater().execute(); |
| 456 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 457 | } |
| 458 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 459 | } |