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