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