Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
Steve McKay | 35820cf | 2015-11-03 14:00:45 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
Aga Wronska | 973168c | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 20 | import static com.android.documentsui.State.ACTION_OPEN_TREE; |
Steve McKay | 35820cf | 2015-11-03 14:00:45 -0800 | [diff] [blame] | 21 | |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 22 | import android.app.Fragment; |
| 23 | import android.app.FragmentManager; |
| 24 | import android.app.FragmentTransaction; |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 25 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 26 | import android.content.Context; |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 27 | import android.content.Intent; |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 28 | import android.content.Loader; |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 29 | import android.content.pm.PackageManager; |
| 30 | import android.content.pm.ResolveInfo; |
Jeff Sharkey | c24bbd4 | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 31 | import android.net.Uri; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 32 | import android.os.Bundle; |
Jeff Sharkey | c24bbd4 | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 33 | import android.provider.Settings; |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 34 | import android.support.annotation.Nullable; |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 35 | import android.text.TextUtils; |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 36 | import android.text.format.Formatter; |
Steve McKay | 35820cf | 2015-11-03 14:00:45 -0800 | [diff] [blame] | 37 | import android.util.Log; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 38 | import android.view.LayoutInflater; |
| 39 | import android.view.View; |
| 40 | import android.view.ViewGroup; |
| 41 | import android.widget.AdapterView; |
| 42 | import android.widget.AdapterView.OnItemClickListener; |
Jeff Sharkey | c24bbd4 | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 43 | import android.widget.AdapterView.OnItemLongClickListener; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 44 | import android.widget.ArrayAdapter; |
| 45 | import android.widget.ImageView; |
| 46 | import android.widget.ListView; |
| 47 | import android.widget.TextView; |
| 48 | |
Jeff Sharkey | ae9b51b | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 49 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 50 | |
Steve McKay | 58efce3 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 51 | import java.util.ArrayList; |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 52 | import java.util.Collection; |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 53 | import java.util.Collections; |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 54 | import java.util.Comparator; |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 55 | import java.util.List; |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 56 | import java.util.Objects; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 57 | |
| 58 | /** |
| 59 | * Display list of known storage backend roots. |
| 60 | */ |
| 61 | public class RootsFragment extends Fragment { |
| 62 | |
Steve McKay | 35820cf | 2015-11-03 14:00:45 -0800 | [diff] [blame] | 63 | private static final String TAG = "RootsFragment"; |
| 64 | private static final String EXTRA_INCLUDE_APPS = "includeApps"; |
| 65 | |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 66 | private ListView mList; |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 67 | private RootsAdapter mAdapter; |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 68 | private LoaderCallbacks<Collection<RootInfo>> mCallbacks; |
| 69 | |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 70 | |
| 71 | public static void show(FragmentManager fm, Intent includeApps) { |
| 72 | final Bundle args = new Bundle(); |
| 73 | args.putParcelable(EXTRA_INCLUDE_APPS, includeApps); |
| 74 | |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 75 | final RootsFragment fragment = new RootsFragment(); |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 76 | fragment.setArguments(args); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 77 | |
| 78 | final FragmentTransaction ft = fm.beginTransaction(); |
| 79 | ft.replace(R.id.container_roots, fragment); |
| 80 | ft.commitAllowingStateLoss(); |
| 81 | } |
| 82 | |
| 83 | public static RootsFragment get(FragmentManager fm) { |
| 84 | return (RootsFragment) fm.findFragmentById(R.id.container_roots); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public View onCreateView( |
| 89 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 90 | |
| 91 | final View view = inflater.inflate(R.layout.fragment_roots, container, false); |
Ben Kwa | 2036dad | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 92 | mList = (ListView) view.findViewById(R.id.roots_list); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 93 | mList.setOnItemClickListener(mItemListener); |
Jeff Sharkey | 28c05ee | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 94 | mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 95 | return view; |
| 96 | } |
| 97 | |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 98 | @Override |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 99 | public void onActivityCreated(Bundle savedInstanceState) { |
| 100 | super.onActivityCreated(savedInstanceState); |
| 101 | |
| 102 | final Context context = getActivity(); |
| 103 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 104 | final State state = ((BaseActivity) context).getDisplayState(); |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 105 | |
| 106 | mCallbacks = new LoaderCallbacks<Collection<RootInfo>>() { |
| 107 | @Override |
| 108 | public Loader<Collection<RootInfo>> onCreateLoader(int id, Bundle args) { |
| 109 | return new RootsLoader(context, roots, state); |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void onLoadFinished( |
| 114 | Loader<Collection<RootInfo>> loader, Collection<RootInfo> result) { |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 115 | if (!isAdded()) { |
| 116 | return; |
| 117 | } |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 118 | |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 119 | Intent handlerAppIntent = getArguments().getParcelable(EXTRA_INCLUDE_APPS); |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 120 | |
Aga Wronska | 973168c | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 121 | mAdapter = new RootsAdapter(context, result, handlerAppIntent, state); |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 122 | mList.setAdapter(mAdapter); |
| 123 | |
| 124 | onCurrentRootChanged(); |
| 125 | } |
| 126 | |
| 127 | @Override |
| 128 | public void onLoaderReset(Loader<Collection<RootInfo>> loader) { |
| 129 | mAdapter = null; |
| 130 | mList.setAdapter(null); |
| 131 | } |
| 132 | }; |
Jeff Sharkey | 348ad68 | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 133 | } |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 134 | |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 135 | @Override |
| 136 | public void onResume() { |
| 137 | super.onResume(); |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 138 | onDisplayStateChanged(); |
| 139 | } |
Jeff Sharkey | 348ad68 | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 140 | |
Jeff Sharkey | 669f8e7 | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 141 | public void onDisplayStateChanged() { |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 142 | final Context context = getActivity(); |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 143 | final State state = ((BaseActivity) context).getDisplayState(); |
Jeff Sharkey | 348ad68 | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 144 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 145 | if (state.action == State.ACTION_GET_CONTENT) { |
Jeff Sharkey | c24bbd4 | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 146 | mList.setOnItemLongClickListener(mItemLongClickListener); |
| 147 | } else { |
| 148 | mList.setOnItemLongClickListener(null); |
| 149 | mList.setLongClickable(false); |
| 150 | } |
| 151 | |
Jeff Sharkey | a9ce049 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 152 | getLoaderManager().restartLoader(2, null, mCallbacks); |
Jeff Sharkey | 28c05ee | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | public void onCurrentRootChanged() { |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 156 | if (mAdapter == null) { |
| 157 | return; |
| 158 | } |
Jeff Sharkey | 28c05ee | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 159 | |
Steve McKay | e934ce6 | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 160 | final RootInfo root = ((BaseActivity) getActivity()).getCurrentRoot(); |
Jeff Sharkey | 28c05ee | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 161 | for (int i = 0; i < mAdapter.getCount(); i++) { |
| 162 | final Object item = mAdapter.getItem(i); |
Jeff Sharkey | a82c2e2 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 163 | if (item instanceof RootItem) { |
| 164 | final RootInfo testRoot = ((RootItem) item).root; |
Kenny Root | e6585b3 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 165 | if (Objects.equals(testRoot, root)) { |
Jeff Sharkey | a82c2e2 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 166 | mList.setItemChecked(i, true); |
Aga Wronska | 8e1c963 | 2016-02-02 15:00:18 -0800 | [diff] [blame] | 167 | mList.setSelection(i); |
Jeff Sharkey | a82c2e2 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 168 | return; |
| 169 | } |
Jeff Sharkey | 28c05ee | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Ben Kwa | 2036dad | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 174 | /** |
| 175 | * Attempts to shift focus back to the navigation drawer. |
| 176 | */ |
| 177 | public void requestFocus() { |
| 178 | mList.requestFocus(); |
| 179 | } |
| 180 | |
Jeff Sharkey | c24bbd4 | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 181 | private void showAppDetails(ResolveInfo ri) { |
| 182 | final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 183 | intent.setData(Uri.fromParts("package", ri.activityInfo.packageName, null)); |
| 184 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
| 185 | startActivity(intent); |
| 186 | } |
| 187 | |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 188 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 189 | @Override |
| 190 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 191 | Item item = mAdapter.getItem(position); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 192 | if (item instanceof RootItem) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 193 | BaseActivity activity = BaseActivity.get(RootsFragment.this); |
Aga Wronska | 45f75e2 | 2016-02-02 18:01:58 -0800 | [diff] [blame] | 194 | RootInfo newRoot = ((RootItem) item).root; |
| 195 | Metrics.logRootVisited(getActivity(), newRoot); |
| 196 | activity.onRootPicked(newRoot); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 197 | } else if (item instanceof AppItem) { |
Steve McKay | ef3e2cf | 2015-04-20 17:18:15 -0700 | [diff] [blame] | 198 | DocumentsActivity activity = DocumentsActivity.get(RootsFragment.this); |
Ben Kwa | 7237998 | 2016-01-26 11:50:03 -0800 | [diff] [blame] | 199 | ResolveInfo info = ((AppItem) item).info; |
| 200 | Metrics.logAppVisited(getActivity(), info); |
| 201 | activity.onAppPicked(info); |
Steve McKay | 35820cf | 2015-11-03 14:00:45 -0800 | [diff] [blame] | 202 | } else if (item instanceof SpacerItem) { |
| 203 | if (DEBUG) Log.d(TAG, "Ignoring click on spacer item."); |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 204 | } else { |
| 205 | throw new IllegalStateException("Unknown root: " + item); |
| 206 | } |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 207 | } |
| 208 | }; |
| 209 | |
Jeff Sharkey | c24bbd4 | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 210 | private OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() { |
| 211 | @Override |
| 212 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 213 | final Item item = mAdapter.getItem(position); |
| 214 | if (item instanceof AppItem) { |
| 215 | showAppDetails(((AppItem) item).info); |
| 216 | return true; |
| 217 | } else { |
| 218 | return false; |
| 219 | } |
| 220 | } |
| 221 | }; |
| 222 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 223 | private static abstract class Item { |
| 224 | private final int mLayoutId; |
| 225 | |
| 226 | public Item(int layoutId) { |
| 227 | mLayoutId = layoutId; |
| 228 | } |
| 229 | |
| 230 | public View getView(View convertView, ViewGroup parent) { |
| 231 | if (convertView == null) { |
| 232 | convertView = LayoutInflater.from(parent.getContext()) |
| 233 | .inflate(mLayoutId, parent, false); |
| 234 | } |
| 235 | bindView(convertView); |
| 236 | return convertView; |
| 237 | } |
| 238 | |
| 239 | public abstract void bindView(View convertView); |
| 240 | } |
| 241 | |
| 242 | private static class RootItem extends Item { |
| 243 | public final RootInfo root; |
| 244 | |
| 245 | public RootItem(RootInfo root) { |
| 246 | super(R.layout.item_root); |
| 247 | this.root = root; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | @Override |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 251 | public void bindView(View convertView) { |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 252 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 253 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 254 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 255 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 256 | final Context context = convertView.getContext(); |
Jeff Sharkey | cbce470 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 257 | icon.setImageDrawable(root.loadDrawerIcon(context)); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 258 | title.setText(root.title); |
| 259 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 260 | // Show available space if no summary |
| 261 | String summaryText = root.summary; |
| 262 | if (TextUtils.isEmpty(summaryText) && root.availableBytes >= 0) { |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 263 | summaryText = context.getString(R.string.root_available_bytes, |
| 264 | Formatter.formatFileSize(context, root.availableBytes)); |
Jeff Sharkey | 9fb567b | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | summary.setText(summaryText); |
Jeff Sharkey | 4ec9739 | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 268 | summary.setVisibility(TextUtils.isEmpty(summaryText) ? View.GONE : View.VISIBLE); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 269 | } |
| 270 | } |
| 271 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 272 | private static class SpacerItem extends Item { |
| 273 | public SpacerItem() { |
| 274 | super(R.layout.item_root_spacer); |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | @Override |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 278 | public void bindView(View convertView) { |
| 279 | // Nothing to bind |
| 280 | } |
| 281 | } |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 282 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 283 | private static class AppItem extends Item { |
| 284 | public final ResolveInfo info; |
| 285 | |
| 286 | public AppItem(ResolveInfo info) { |
| 287 | super(R.layout.item_root); |
| 288 | this.info = info; |
| 289 | } |
| 290 | |
| 291 | @Override |
| 292 | public void bindView(View convertView) { |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 293 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 294 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 295 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 296 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 297 | final PackageManager pm = convertView.getContext().getPackageManager(); |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 298 | icon.setImageDrawable(info.loadIcon(pm)); |
| 299 | title.setText(info.loadLabel(pm)); |
| 300 | |
| 301 | // TODO: match existing summary behavior from disambig dialog |
| 302 | summary.setVisibility(View.GONE); |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 306 | private static class RootsAdapter extends ArrayAdapter<Item> { |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 307 | |
| 308 | /** |
| 309 | * @param handlerAppIntent When not null, apps capable of handling the original |
| 310 | * intent will be included in list of roots (in special section at bottom). |
| 311 | */ |
Aga Wronska | 973168c | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 312 | public RootsAdapter(Context context, Collection<RootInfo> roots, |
| 313 | @Nullable Intent handlerAppIntent, State state) { |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 314 | super(context, 0); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 315 | |
Ben Kwa | 0c64308 | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 316 | final List<RootItem> libraries = new ArrayList<>(); |
Daichi Hirono | abf3974 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 317 | final List<RootItem> others = new ArrayList<>(); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 318 | |
Daichi Hirono | abf3974 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 319 | for (final RootInfo root : roots) { |
| 320 | final RootItem item = new RootItem(root); |
Aga Wronska | b4129d7 | 2016-03-17 11:48:20 -0700 | [diff] [blame] | 321 | |
Aga Wronska | 4e62716 | 2016-03-22 14:18:43 -0700 | [diff] [blame] | 322 | if (root.isHome() && Shared.isHomeRootHidden(context)) { |
Aga Wronska | b4129d7 | 2016-03-17 11:48:20 -0700 | [diff] [blame] | 323 | continue; |
Aga Wronska | 973168c | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 324 | } else if (root.isAdvanced() |
| 325 | && Shared.areAdvancedRootsHidden(context, state)) { |
Aga Wronska | 1719b35 | 2016-03-21 11:28:03 -0700 | [diff] [blame] | 326 | continue; |
Aga Wronska | b4129d7 | 2016-03-17 11:48:20 -0700 | [diff] [blame] | 327 | } else if (root.isLibrary()) { |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 328 | if (DEBUG) Log.d(TAG, "Adding " + root + " as library."); |
Daichi Hirono | abf3974 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 329 | libraries.add(item); |
| 330 | } else { |
Steve McKay | 3ce9595 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 331 | if (DEBUG) Log.d(TAG, "Adding " + root + " as non-library."); |
Daichi Hirono | abf3974 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 332 | others.add(item); |
Jeff Sharkey | 5545f56 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 333 | } |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 334 | } |
Jeff Sharkey | 5545f56 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 335 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 336 | final RootComparator comp = new RootComparator(); |
Ben Kwa | 0c64308 | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 337 | Collections.sort(libraries, comp); |
Daichi Hirono | abf3974 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 338 | Collections.sort(others, comp); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 339 | |
Ben Kwa | 0c64308 | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 340 | addAll(libraries); |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 341 | // Only add the spacer if it is actually separating something. |
| 342 | if (!libraries.isEmpty() && !others.isEmpty()) { |
| 343 | add(new SpacerItem()); |
| 344 | } |
Daichi Hirono | abf3974 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 345 | addAll(others); |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 346 | |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 347 | // Include apps that can handle this intent too. |
| 348 | if (handlerAppIntent != null) { |
| 349 | includeHandlerApps(context, handlerAppIntent); |
| 350 | } |
| 351 | } |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 352 | |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 353 | /** |
| 354 | * Adds apps capable of handling the original intent will be included |
| 355 | * in list of roots (in special section at bottom). |
| 356 | */ |
| 357 | private void includeHandlerApps(Context context, Intent handlerAppIntent) { |
| 358 | final PackageManager pm = context.getPackageManager(); |
| 359 | final List<ResolveInfo> infos = pm.queryIntentActivities( |
| 360 | handlerAppIntent, PackageManager.MATCH_DEFAULT_ONLY); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 361 | |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 362 | final List<AppItem> apps = new ArrayList<>(); |
| 363 | |
| 364 | // Omit ourselves from the list |
| 365 | for (ResolveInfo info : infos) { |
| 366 | if (!context.getPackageName().equals(info.activityInfo.packageName)) { |
| 367 | apps.add(new AppItem(info)); |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 368 | } |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 369 | } |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 370 | |
Steve McKay | 4a1ca86 | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 371 | if (apps.size() > 0) { |
| 372 | add(new SpacerItem()); |
| 373 | addAll(apps); |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 374 | } |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 375 | } |
Jeff Sharkey | 54ca29a | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 376 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 377 | @Override |
| 378 | public View getView(int position, View convertView, ViewGroup parent) { |
| 379 | final Item item = getItem(position); |
| 380 | return item.getView(convertView, parent); |
| 381 | } |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 382 | |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 383 | @Override |
| 384 | public boolean areAllItemsEnabled() { |
| 385 | return false; |
| 386 | } |
| 387 | |
| 388 | @Override |
| 389 | public boolean isEnabled(int position) { |
| 390 | return getItemViewType(position) != 1; |
| 391 | } |
| 392 | |
| 393 | @Override |
| 394 | public int getItemViewType(int position) { |
| 395 | final Item item = getItem(position); |
| 396 | if (item instanceof RootItem || item instanceof AppItem) { |
| 397 | return 0; |
| 398 | } else { |
| 399 | return 1; |
Jeff Sharkey | 5545f56 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 400 | } |
Jeff Sharkey | 6efba22 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | @Override |
| 404 | public int getViewTypeCount() { |
| 405 | return 2; |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 406 | } |
| 407 | } |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 408 | |
Ben Kwa | 0c64308 | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 409 | public static class RootComparator implements Comparator<RootItem> { |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 410 | @Override |
Ben Kwa | 0c64308 | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 411 | public int compare(RootItem lhs, RootItem rhs) { |
Steve McKay | 008e948 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 412 | return lhs.root.compareTo(rhs.root); |
Jeff Sharkey | aeb16e2 | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
Jeff Sharkey | 6651669 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 415 | } |