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