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