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 | |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 55 | static final String EXTRA_SHOW_MENU = "show_drawer_menu"; |
| 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 { |
| 158 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 159 | mDrawerLayout = null; |
| 160 | } |
| 161 | } |
| 162 | |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 163 | public void openDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 164 | if (mDrawerLayout != null) { |
| 165 | mDrawerLayout.openDrawer(Gravity.START); |
| 166 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | public void closeDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 170 | if (mDrawerLayout != null) { |
| 171 | mDrawerLayout.closeDrawers(); |
| 172 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void setContentView(@LayoutRes int layoutResID) { |
| 177 | LayoutInflater.from(this).inflate(layoutResID, |
| 178 | (ViewGroup) findViewById(R.id.content_frame)); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | public void setContentView(View view) { |
| 183 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view); |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public void setContentView(View view, ViewGroup.LayoutParams params) { |
| 188 | ((ViewGroup) findViewById(R.id.content_frame)).addView(view, params); |
| 189 | } |
| 190 | |
| 191 | public void updateDrawer() { |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 192 | if (mDrawerLayout == null) { |
| 193 | return; |
| 194 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 195 | // TODO: Do this in the background with some loading. |
| 196 | mDrawerAdapter.updateCategories(); |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 197 | if (mDrawerAdapter.getCount() != 0) { |
| 198 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); |
Jason Monk | 51a9423 | 2015-11-04 14:57:15 -0500 | [diff] [blame] | 199 | } else { |
| 200 | mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); |
| 201 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 202 | } |
| 203 | |
Jason Monk | 946483a | 2016-01-22 15:19:44 -0500 | [diff] [blame] | 204 | public void showMenuIcon() { |
| 205 | mShowingMenu = true; |
| 206 | getActionBar().setHomeAsUpIndicator(R.drawable.ic_menu); |
| 207 | getActionBar().setDisplayHomeAsUpEnabled(true); |
| 208 | } |
| 209 | |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 210 | public List<DashboardCategory> getDashboardCategories() { |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 211 | if (sDashboardCategories == null) { |
| 212 | sTileCache = new HashMap<>(); |
Jason Monk | ec02e47 | 2016-03-28 10:44:43 -0400 | [diff] [blame] | 213 | sConfigTracker = new InterestingConfigChanges(); |
Jason Monk | 2a5d79a | 2015-12-03 10:51:58 -0500 | [diff] [blame] | 214 | sDashboardCategories = TileUtils.getCategories(this, sTileCache); |
| 215 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 216 | return sDashboardCategories; |
| 217 | } |
| 218 | |
| 219 | protected void onCategoriesChanged() { |
| 220 | updateDrawer(); |
| 221 | final int N = mCategoryListeners.size(); |
| 222 | for (int i = 0; i < N; i++) { |
| 223 | mCategoryListeners.get(i).onCategoriesChanged(); |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 224 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Jason Monk | f509d7e | 2016-01-07 16:22:53 -0500 | [diff] [blame] | 227 | public boolean openTile(Tile tile) { |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 228 | closeDrawer(); |
Jason Monk | 3175a6e | 2015-11-30 15:05:35 -0500 | [diff] [blame] | 229 | if (tile == null) { |
Jason Monk | cafda1f | 2016-02-05 10:16:22 -0500 | [diff] [blame] | 230 | startActivity(new Intent(Settings.ACTION_SETTINGS).addFlags( |
| 231 | Intent.FLAG_ACTIVITY_CLEAR_TASK)); |
| 232 | return true; |
Jason Monk | 3175a6e | 2015-11-30 15:05:35 -0500 | [diff] [blame] | 233 | } |
Jason Monk | 6bea950 | 2016-02-25 13:07:41 -0500 | [diff] [blame] | 234 | try { |
| 235 | int numUserHandles = tile.userHandle.size(); |
| 236 | if (numUserHandles > 1) { |
| 237 | ProfileSelectDialog.show(getFragmentManager(), tile); |
| 238 | return false; |
| 239 | } else if (numUserHandles == 1) { |
| 240 | // Show menu on top level items. |
| 241 | tile.intent.putExtra(EXTRA_SHOW_MENU, true); |
| 242 | tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 243 | startActivityAsUser(tile.intent, tile.userHandle.get(0)); |
| 244 | } else { |
| 245 | // Show menu on top level items. |
| 246 | tile.intent.putExtra(EXTRA_SHOW_MENU, true); |
| 247 | tile.intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); |
| 248 | startActivity(tile.intent); |
| 249 | } |
| 250 | } catch (ActivityNotFoundException e) { |
| 251 | Log.w(TAG, "Couldn't find tile " + tile.intent, e); |
Jason Monk | 0d72d20 | 2015-11-04 13:16:00 -0500 | [diff] [blame] | 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) { |
Jason Monk | ec02e47 | 2016-03-28 10:44:43 -0400 | [diff] [blame] | 273 | if (sConfigTracker.applyNewConfig(getResources())) { |
| 274 | sTileCache.clear(); |
| 275 | } |
Jason Monk | e79790b | 2015-12-02 15:39:19 -0500 | [diff] [blame] | 276 | return TileUtils.getCategories(SettingsDrawerActivity.this, sTileCache); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | protected void onPostExecute(List<DashboardCategory> dashboardCategories) { |
| 281 | sDashboardCategories = dashboardCategories; |
| 282 | onCategoriesChanged(); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | private class PackageReceiver extends BroadcastReceiver { |
| 287 | @Override |
| 288 | public void onReceive(Context context, Intent intent) { |
| 289 | new CategoriesUpdater().execute(); |
| 290 | } |
| 291 | } |
Jason Monk | 744b636 | 2015-11-03 18:24:29 -0500 | [diff] [blame] | 292 | } |