Jeff Sharkey | 7611221 | 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 | cc460af | 2015-11-03 14:00:45 -0800 | [diff] [blame] | 19 | import static com.android.documentsui.Shared.DEBUG; |
| 20 | |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 21 | import android.annotation.LayoutRes; |
Steve McKay | 17f7e58 | 2016-04-04 15:26:48 -0700 | [diff] [blame] | 22 | import android.app.Activity; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 23 | import android.app.Fragment; |
| 24 | import android.app.FragmentManager; |
| 25 | import android.app.FragmentTransaction; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 26 | import android.app.LoaderManager.LoaderCallbacks; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 27 | import android.content.Context; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 28 | import android.content.Intent; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 29 | import android.content.Loader; |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 30 | import android.content.pm.ActivityInfo; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 31 | import android.content.pm.PackageManager; |
| 32 | import android.content.pm.ResolveInfo; |
Jeff Sharkey | 8f1edfe | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 33 | import android.net.Uri; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 34 | import android.os.Bundle; |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 35 | import android.os.Looper; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 36 | import android.provider.DocumentsContract.Root; |
| 37 | import android.provider.DocumentsContract; |
Jeff Sharkey | 8f1edfe | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 38 | import android.provider.Settings; |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 39 | import android.support.annotation.Nullable; |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 40 | import android.text.TextUtils; |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 41 | import android.text.format.Formatter; |
Steve McKay | cc460af | 2015-11-03 14:00:45 -0800 | [diff] [blame] | 42 | import android.util.Log; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 43 | import android.view.ContextMenu; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 44 | import android.view.LayoutInflater; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 45 | import android.view.Menu; |
| 46 | import android.view.MenuInflater; |
| 47 | import android.view.MenuItem; |
| 48 | import android.view.MotionEvent; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 49 | import android.view.View; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 50 | import android.view.View.OnClickListener; |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 51 | import android.view.View.OnDragListener; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 52 | import android.view.View.OnGenericMotionListener; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 53 | import android.view.ViewGroup; |
| 54 | import android.widget.AdapterView; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 55 | import android.widget.AdapterView.AdapterContextMenuInfo; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 56 | import android.widget.AdapterView.OnItemClickListener; |
Jeff Sharkey | 8f1edfe | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 57 | import android.widget.AdapterView.OnItemLongClickListener; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 58 | import android.widget.ArrayAdapter; |
| 59 | import android.widget.ImageView; |
| 60 | import android.widget.ListView; |
| 61 | import android.widget.TextView; |
| 62 | |
Jeff Sharkey | 724deeb | 2013-08-31 15:02:20 -0700 | [diff] [blame] | 63 | import com.android.documentsui.model.RootInfo; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 64 | |
Steve McKay | fefcd70 | 2015-08-20 16:19:38 +0000 | [diff] [blame] | 65 | import java.util.ArrayList; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 66 | import java.util.Collection; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 67 | import java.util.Collections; |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 68 | import java.util.Comparator; |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 69 | import java.util.HashMap; |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 70 | import java.util.List; |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 71 | import java.util.Map; |
Kenny Root | 16d8598 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 72 | import java.util.Objects; |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 73 | import java.util.function.BooleanSupplier; |
| 74 | import java.util.function.Consumer; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 75 | |
| 76 | /** |
| 77 | * Display list of known storage backend roots. |
| 78 | */ |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 79 | public class RootsFragment extends Fragment implements ItemDragListener.DragHost { |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 80 | |
Steve McKay | cc460af | 2015-11-03 14:00:45 -0800 | [diff] [blame] | 81 | private static final String TAG = "RootsFragment"; |
| 82 | private static final String EXTRA_INCLUDE_APPS = "includeApps"; |
| 83 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 84 | private ListView mList; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 85 | private RootsAdapter mAdapter; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 86 | private LoaderCallbacks<Collection<RootInfo>> mCallbacks; |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 87 | private Consumer<RootInfo> mOpenSettings = (RootInfo) -> { |
| 88 | throw new UnsupportedOperationException("Can't open settings."); |
| 89 | }; |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 90 | |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 91 | public static void show(FragmentManager fm, Consumer<RootInfo> openSettings) { |
| 92 | RootsFragment fragment = show(fm, (Intent) null); |
| 93 | fragment.mOpenSettings = openSettings; |
| 94 | } |
| 95 | |
| 96 | public static RootsFragment show(FragmentManager fm, Intent includeApps) { |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 97 | final Bundle args = new Bundle(); |
| 98 | args.putParcelable(EXTRA_INCLUDE_APPS, includeApps); |
| 99 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 100 | final RootsFragment fragment = new RootsFragment(); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 101 | fragment.setArguments(args); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 102 | |
| 103 | final FragmentTransaction ft = fm.beginTransaction(); |
| 104 | ft.replace(R.id.container_roots, fragment); |
| 105 | ft.commitAllowingStateLoss(); |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 106 | |
| 107 | return fragment; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | public static RootsFragment get(FragmentManager fm) { |
| 111 | return (RootsFragment) fm.findFragmentById(R.id.container_roots); |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public View onCreateView( |
| 116 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 117 | |
| 118 | final View view = inflater.inflate(R.layout.fragment_roots, container, false); |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 119 | mList = (ListView) view.findViewById(R.id.roots_list); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 120 | mList.setOnItemClickListener(mItemListener); |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 121 | // ListView does not have right-click specific listeners, so we will have a |
| 122 | // GenericMotionListener to listen for it. |
| 123 | // Currently, right click is viewed the same as long press, so we will have to quickly |
| 124 | // register for context menu when we receive a right click event, and quickly unregister |
| 125 | // it afterwards to prevent context menus popping up upon long presses. |
| 126 | // All other motion events will then get passed to OnItemClickListener. |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 127 | mList.setOnGenericMotionListener( |
| 128 | new OnGenericMotionListener() { |
| 129 | @Override |
| 130 | public boolean onGenericMotion(View v, MotionEvent event) { |
| 131 | if (Events.isMouseEvent(event) |
| 132 | && event.getButtonState() == MotionEvent.BUTTON_SECONDARY) { |
| 133 | registerForContextMenu(v); |
| 134 | v.showContextMenu(event.getX(), event.getY()); |
| 135 | unregisterForContextMenu(v); |
| 136 | return true; |
| 137 | } |
| 138 | return false; |
| 139 | } |
| 140 | }); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 141 | mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 142 | return view; |
| 143 | } |
| 144 | |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 145 | @Override |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 146 | public void onActivityCreated(Bundle savedInstanceState) { |
| 147 | super.onActivityCreated(savedInstanceState); |
| 148 | |
| 149 | final Context context = getActivity(); |
| 150 | final RootsCache roots = DocumentsApplication.getRootsCache(context); |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 151 | final State state = ((BaseActivity) context).getDisplayState(); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 152 | |
| 153 | mCallbacks = new LoaderCallbacks<Collection<RootInfo>>() { |
| 154 | @Override |
| 155 | public Loader<Collection<RootInfo>> onCreateLoader(int id, Bundle args) { |
| 156 | return new RootsLoader(context, roots, state); |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public void onLoadFinished( |
| 161 | Loader<Collection<RootInfo>> loader, Collection<RootInfo> result) { |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 162 | if (!isAdded()) { |
| 163 | return; |
| 164 | } |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 165 | |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 166 | Intent handlerAppIntent = getArguments().getParcelable(EXTRA_INCLUDE_APPS); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 167 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 168 | mAdapter = new RootsAdapter(context, result, handlerAppIntent, state, |
| 169 | new ItemDragListener<>(RootsFragment.this)); |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 170 | mList.setAdapter(mAdapter); |
| 171 | |
| 172 | onCurrentRootChanged(); |
| 173 | } |
| 174 | |
| 175 | @Override |
| 176 | public void onLoaderReset(Loader<Collection<RootInfo>> loader) { |
| 177 | mAdapter = null; |
| 178 | mList.setAdapter(null); |
| 179 | } |
| 180 | }; |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 181 | } |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 182 | |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 183 | @Override |
| 184 | public void onResume() { |
| 185 | super.onResume(); |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 186 | onDisplayStateChanged(); |
| 187 | } |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 188 | |
Jeff Sharkey | e8d13ea | 2014-08-08 15:10:03 -0700 | [diff] [blame] | 189 | public void onDisplayStateChanged() { |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 190 | final Context context = getActivity(); |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 191 | final State state = ((BaseActivity) context).getDisplayState(); |
Jeff Sharkey | 1c903cc | 2013-09-02 17:19:40 -0700 | [diff] [blame] | 192 | |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 193 | if (state.action == State.ACTION_GET_CONTENT) { |
Jeff Sharkey | 8f1edfe | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 194 | mList.setOnItemLongClickListener(mItemLongClickListener); |
| 195 | } else { |
| 196 | mList.setOnItemLongClickListener(null); |
| 197 | mList.setLongClickable(false); |
| 198 | } |
| 199 | |
Jeff Sharkey | 8b99704 | 2013-09-19 15:25:56 -0700 | [diff] [blame] | 200 | getLoaderManager().restartLoader(2, null, mCallbacks); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | public void onCurrentRootChanged() { |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 204 | if (mAdapter == null) { |
| 205 | return; |
| 206 | } |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 207 | |
Steve McKay | d0a2a2c | 2015-03-25 14:35:33 -0700 | [diff] [blame] | 208 | final RootInfo root = ((BaseActivity) getActivity()).getCurrentRoot(); |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 209 | for (int i = 0; i < mAdapter.getCount(); i++) { |
| 210 | final Object item = mAdapter.getItem(i); |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 211 | if (item instanceof RootItem) { |
| 212 | final RootInfo testRoot = ((RootItem) item).root; |
Kenny Root | 16d8598 | 2013-12-13 12:00:26 -0800 | [diff] [blame] | 213 | if (Objects.equals(testRoot, root)) { |
Jeff Sharkey | 25f10b3 | 2013-10-07 14:08:17 -0700 | [diff] [blame] | 214 | mList.setItemChecked(i, true); |
| 215 | return; |
| 216 | } |
Jeff Sharkey | 42d2679 | 2013-09-06 13:22:09 -0700 | [diff] [blame] | 217 | } |
| 218 | } |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Ben Kwa | 74e5d41 | 2016-02-10 07:46:35 -0800 | [diff] [blame] | 221 | /** |
| 222 | * Attempts to shift focus back to the navigation drawer. |
| 223 | */ |
| 224 | public void requestFocus() { |
| 225 | mList.requestFocus(); |
| 226 | } |
| 227 | |
Jeff Sharkey | 8f1edfe | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 228 | private void showAppDetails(ResolveInfo ri) { |
| 229 | final Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS); |
| 230 | intent.setData(Uri.fromParts("package", ri.activityInfo.packageName, null)); |
| 231 | intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET); |
| 232 | startActivity(intent); |
| 233 | } |
| 234 | |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 235 | private BaseActivity getBaseActivity() { |
| 236 | return (BaseActivity) getActivity(); |
| 237 | } |
| 238 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 239 | @Override |
| 240 | public void runOnUiThread(Runnable runnable) { |
| 241 | getActivity().runOnUiThread(runnable); |
| 242 | } |
| 243 | |
| 244 | /** |
| 245 | * {@inheritDoc} |
| 246 | * |
| 247 | * In RootsFragment we open the hovered root. |
| 248 | */ |
| 249 | @Override |
| 250 | public void onViewHovered(View view) { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 251 | // SpacerView doesn't have DragListener so this view is guaranteed to be a RootItemView. |
| 252 | RootItemView itemView = (RootItemView) view; |
| 253 | itemView.drawRipple(); |
| 254 | |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 255 | final int position = (Integer) view.getTag(R.id.item_position_tag); |
| 256 | final Item item = mAdapter.getItem(position); |
| 257 | item.open(this); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | @Override |
| 261 | public void setDropTargetHighlight(View v, boolean highlight) { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 262 | // SpacerView doesn't have DragListener so this view is guaranteed to be a RootItemView. |
| 263 | RootItemView itemView = (RootItemView) v; |
| 264 | itemView.setHighlight(highlight); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 265 | } |
| 266 | |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 267 | @Override |
| 268 | public void onCreateContextMenu( |
| 269 | ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { |
| 270 | super.onCreateContextMenu(menu, v, menuInfo); |
| 271 | AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) menuInfo; |
| 272 | final Item item = mAdapter.getItem(adapterMenuInfo.position); |
| 273 | if (item instanceof RootItem) { |
| 274 | RootItem rootItem = (RootItem) item; |
| 275 | MenuInflater inflater = getActivity().getMenuInflater(); |
| 276 | inflater.inflate(R.menu.root_context_menu, menu); |
| 277 | (getBaseActivity()).getMenuManager().updateRootContextMenu(menu, rootItem.root); |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | @Override |
| 282 | public boolean onContextItemSelected(MenuItem item) { |
| 283 | AdapterContextMenuInfo adapterMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo(); |
Ben Lin | 035f0c8 | 2016-07-14 10:40:00 -0700 | [diff] [blame^] | 284 | // There is a possibility that this is called from DirectoryFragment since |
| 285 | // all fragments' onContextItemSelected gets called when any menu item is selected |
| 286 | // This is to guard against it since DirectoryFragment's RecylerView does not have a |
| 287 | // menuInfo |
| 288 | if (adapterMenuInfo == null) { |
| 289 | return false; |
| 290 | } |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 291 | final RootItem rootItem = (RootItem) mAdapter.getItem(adapterMenuInfo.position); |
Ben Lin | 035f0c8 | 2016-07-14 10:40:00 -0700 | [diff] [blame^] | 292 | switch (item.getItemId()) { |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 293 | case R.id.menu_eject_root: |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 294 | final View ejectIcon = adapterMenuInfo.targetView.findViewById(R.id.eject_icon); |
| 295 | ejectClicked(ejectIcon, rootItem.root); |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 296 | return true; |
| 297 | case R.id.menu_settings: |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 298 | mOpenSettings.accept(rootItem.root); |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 299 | return true; |
| 300 | default: |
| 301 | if (DEBUG) Log.d(TAG, "Unhandled menu item selected: " + item); |
| 302 | return false; |
| 303 | } |
| 304 | } |
| 305 | |
| 306 | private static void ejectClicked(View ejectIcon, RootInfo root) { |
| 307 | assert(ejectIcon != null); |
| 308 | assert(ejectIcon.getContext() instanceof BaseActivity); |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 309 | assert (!root.ejecting); |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 310 | ejectIcon.setEnabled(false); |
| 311 | root.ejecting = true; |
| 312 | ejectRoot( |
| 313 | ejectIcon, |
| 314 | root.authority, |
| 315 | root.rootId, |
| 316 | new Consumer<Boolean>() { |
| 317 | @Override |
| 318 | public void accept(Boolean ejected) { |
| 319 | ejectIcon.setEnabled(!ejected); |
| 320 | root.ejecting = false; |
| 321 | } |
| 322 | }); |
| 323 | } |
| 324 | |
| 325 | static void ejectRoot( |
| 326 | View ejectIcon, String authority, String rootId, Consumer<Boolean> listener) { |
| 327 | BooleanSupplier predicate = () -> { |
| 328 | return !(ejectIcon.getVisibility() == View.VISIBLE); |
| 329 | }; |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 330 | new EjectRootTask(ejectIcon.getContext(), |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 331 | authority, |
| 332 | rootId, |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 333 | predicate, |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 334 | listener).executeOnExecutor(ProviderExecutor.forAuthority(authority)); |
| 335 | } |
| 336 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 337 | private OnItemClickListener mItemListener = new OnItemClickListener() { |
| 338 | @Override |
| 339 | public void onItemClick(AdapterView<?> parent, View view, int position, long id) { |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 340 | final Item item = mAdapter.getItem(position); |
| 341 | item.open(RootsFragment.this); |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 342 | |
| 343 | ((BaseActivity) getActivity()).setRootsDrawerOpen(false); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 344 | } |
| 345 | }; |
| 346 | |
Jeff Sharkey | 8f1edfe | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 347 | private OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() { |
| 348 | @Override |
| 349 | public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { |
| 350 | final Item item = mAdapter.getItem(position); |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 351 | return item.showAppDetails(RootsFragment.this); |
Jeff Sharkey | 8f1edfe | 2013-10-23 16:05:56 -0700 | [diff] [blame] | 352 | } |
| 353 | }; |
| 354 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 355 | private static abstract class Item { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 356 | private final @LayoutRes int mLayoutId; |
| 357 | private final String mStringId; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 358 | |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 359 | public Item(@LayoutRes int layoutId, String stringId) { |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 360 | mLayoutId = layoutId; |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 361 | mStringId = stringId; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | public View getView(View convertView, ViewGroup parent) { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 365 | if (convertView == null |
| 366 | || (Integer) convertView.getTag(R.id.layout_id_tag) != mLayoutId) { |
| 367 | convertView = LayoutInflater.from(parent.getContext()) |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 368 | .inflate(mLayoutId, parent, false); |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 369 | } |
| 370 | convertView.setTag(R.id.layout_id_tag, mLayoutId); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 371 | bindView(convertView); |
| 372 | return convertView; |
| 373 | } |
| 374 | |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 375 | boolean showAppDetails(RootsFragment fragment) { |
| 376 | return false; |
| 377 | } |
| 378 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 379 | abstract void bindView(View convertView); |
| 380 | |
| 381 | abstract boolean isDropTarget(); |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 382 | |
| 383 | abstract void open(RootsFragment fragment); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | private static class RootItem extends Item { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 387 | private static final String STRING_ID_FORMAT = "RootItem{%s/%s}"; |
| 388 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 389 | public final RootInfo root; |
| 390 | |
| 391 | public RootItem(RootInfo root) { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 392 | super(R.layout.item_root, getStringId(root)); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 393 | this.root = root; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 394 | } |
| 395 | |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 396 | private static String getStringId(RootInfo root) { |
| 397 | // Empty URI authority is invalid, so we can use empty string if root.authority is null. |
| 398 | // Directly passing null to String.format() will write "null" which can be a valid URI |
| 399 | // authority. |
| 400 | String authority = (root.authority == null ? "" : root.authority); |
| 401 | return String.format(STRING_ID_FORMAT, authority, root.rootId); |
| 402 | } |
| 403 | |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 404 | @Override |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 405 | public void bindView(View convertView) { |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 406 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 407 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 408 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 409 | final ImageView ejectIcon = (ImageView) convertView.findViewById(R.id.eject_icon); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 410 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 411 | final Context context = convertView.getContext(); |
Jeff Sharkey | 7e54461 | 2014-08-29 15:38:27 -0700 | [diff] [blame] | 412 | icon.setImageDrawable(root.loadDrawerIcon(context)); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 413 | title.setText(root.title); |
| 414 | |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 415 | if (root.supportsEject()) { |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 416 | ejectIcon.setVisibility(View.VISIBLE); |
| 417 | ejectIcon.setImageDrawable(root.loadEjectIcon(context)); |
| 418 | ejectIcon.setOnClickListener(new OnClickListener() { |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 419 | @Override |
| 420 | public void onClick(View unmountIcon) { |
| 421 | RootsFragment.ejectClicked(unmountIcon, root); |
| 422 | } |
| 423 | }); |
| 424 | } else { |
Ben Lin | 821727a | 2016-07-08 10:49:53 -0700 | [diff] [blame] | 425 | ejectIcon.setVisibility(View.GONE); |
| 426 | ejectIcon.setOnClickListener(null); |
Ben Lin | 5668b86 | 2016-06-28 17:12:52 -0700 | [diff] [blame] | 427 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 428 | // Show available space if no summary |
| 429 | String summaryText = root.summary; |
| 430 | if (TextUtils.isEmpty(summaryText) && root.availableBytes >= 0) { |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 431 | summaryText = context.getString(R.string.root_available_bytes, |
| 432 | Formatter.formatFileSize(context, root.availableBytes)); |
Jeff Sharkey | c6cbdf1 | 2013-08-07 16:22:02 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | summary.setText(summaryText); |
Jeff Sharkey | a35ac2d | 2013-09-10 12:04:26 -0700 | [diff] [blame] | 436 | summary.setVisibility(TextUtils.isEmpty(summaryText) ? View.GONE : View.VISIBLE); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 437 | } |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 438 | |
| 439 | @Override |
| 440 | boolean isDropTarget() { |
| 441 | return root.supportsCreate() && !root.isLibrary(); |
| 442 | } |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 443 | |
| 444 | @Override |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 445 | void open(RootsFragment fragment) { |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 446 | BaseActivity activity = BaseActivity.get(fragment); |
| 447 | Metrics.logRootVisited(fragment.getActivity(), root); |
| 448 | activity.onRootPicked(root); |
| 449 | } |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 452 | private static class SpacerItem extends Item { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 453 | private static final String STRING_ID = "SpacerItem"; |
| 454 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 455 | public SpacerItem() { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 456 | // Multiple spacer items can share the same string id as they're identical. |
| 457 | super(R.layout.item_root_spacer, STRING_ID); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | @Override |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 461 | void bindView(View convertView) { |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 462 | // Nothing to bind |
| 463 | } |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 464 | |
| 465 | @Override |
| 466 | boolean isDropTarget() { |
| 467 | return false; |
| 468 | } |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 469 | |
| 470 | @Override |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 471 | void open(RootsFragment fragment) { |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 472 | if (DEBUG) Log.d(TAG, "Ignoring click/hover on spacer item."); |
| 473 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 474 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 475 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 476 | private static class AppItem extends Item { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 477 | private static final String STRING_ID_FORMAT = "AppItem{%s/%s}"; |
| 478 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 479 | public final ResolveInfo info; |
| 480 | |
| 481 | public AppItem(ResolveInfo info) { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 482 | super(R.layout.item_root, getStringId(info)); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 483 | this.info = info; |
| 484 | } |
| 485 | |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 486 | private static String getStringId(ResolveInfo info) { |
| 487 | ActivityInfo activityInfo = info.activityInfo; |
| 488 | |
| 489 | String component = String.format( |
| 490 | STRING_ID_FORMAT, activityInfo.applicationInfo.packageName, activityInfo.name); |
| 491 | return component; |
| 492 | } |
| 493 | |
| 494 | @Override |
| 495 | boolean showAppDetails(RootsFragment fragment) { |
| 496 | fragment.showAppDetails(info); |
| 497 | return true; |
| 498 | } |
| 499 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 500 | @Override |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 501 | void bindView(View convertView) { |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 502 | final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon); |
| 503 | final TextView title = (TextView) convertView.findViewById(android.R.id.title); |
| 504 | final TextView summary = (TextView) convertView.findViewById(android.R.id.summary); |
| 505 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 506 | final PackageManager pm = convertView.getContext().getPackageManager(); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 507 | icon.setImageDrawable(info.loadIcon(pm)); |
| 508 | title.setText(info.loadLabel(pm)); |
| 509 | |
| 510 | // TODO: match existing summary behavior from disambig dialog |
| 511 | summary.setVisibility(View.GONE); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 512 | } |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 513 | |
| 514 | @Override |
| 515 | boolean isDropTarget() { |
| 516 | // We won't support drag n' drop in DocumentsActivity, and apps only show up there. |
| 517 | return false; |
| 518 | } |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 519 | |
| 520 | @Override |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 521 | void open(RootsFragment fragment) { |
Garfield, Tan | 7d66a86 | 2016-05-11 10:28:41 -0700 | [diff] [blame] | 522 | DocumentsActivity activity = DocumentsActivity.get(fragment); |
| 523 | Metrics.logAppVisited(fragment.getActivity(), info); |
| 524 | activity.onAppPicked(info); |
| 525 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 526 | } |
| 527 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 528 | private static class RootsAdapter extends ArrayAdapter<Item> { |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 529 | private static final Map<String, Long> sIdMap = new HashMap<String, Long>(); |
| 530 | // the next available id to associate with a new string id |
| 531 | private static long sNextAvailableId; |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 532 | |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 533 | private OnDragListener mDragListener; |
| 534 | |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 535 | /** |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 536 | * @param handlerAppIntent When not null, apps capable of handling the original intent will |
| 537 | * be included in list of roots (in special section at bottom). |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 538 | */ |
Aga Wronska | 6d50bcc | 2016-03-28 17:27:02 -0700 | [diff] [blame] | 539 | public RootsAdapter(Context context, Collection<RootInfo> roots, |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 540 | @Nullable Intent handlerAppIntent, State state, OnDragListener dragListener) { |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 541 | super(context, 0); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 542 | |
Ben Kwa | 85c8cdd | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 543 | final List<RootItem> libraries = new ArrayList<>(); |
Daichi Hirono | 9fb3389 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 544 | final List<RootItem> others = new ArrayList<>(); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 545 | |
Daichi Hirono | 9fb3389 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 546 | for (final RootInfo root : roots) { |
| 547 | final RootItem item = new RootItem(root); |
Aga Wronska | 8453b96 | 2016-03-17 11:48:20 -0700 | [diff] [blame] | 548 | |
Steve McKay | 17f7e58 | 2016-04-04 15:26:48 -0700 | [diff] [blame] | 549 | if (root.isHome() && |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 550 | !Shared.shouldShowDocumentsRoot(context, |
| 551 | ((Activity) context).getIntent())) { |
Aga Wronska | 8453b96 | 2016-03-17 11:48:20 -0700 | [diff] [blame] | 552 | continue; |
| 553 | } else if (root.isLibrary()) { |
Steve McKay | d480081 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 554 | if (DEBUG) Log.d(TAG, "Adding " + root + " as library."); |
Daichi Hirono | 9fb3389 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 555 | libraries.add(item); |
| 556 | } else { |
Steve McKay | d480081 | 2016-02-02 11:41:03 -0800 | [diff] [blame] | 557 | if (DEBUG) Log.d(TAG, "Adding " + root + " as non-library."); |
Daichi Hirono | 9fb3389 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 558 | others.add(item); |
Jeff Sharkey | 4045780 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 559 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 560 | } |
Jeff Sharkey | 4045780 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 561 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 562 | final RootComparator comp = new RootComparator(); |
Ben Kwa | 85c8cdd | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 563 | Collections.sort(libraries, comp); |
Daichi Hirono | 9fb3389 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 564 | Collections.sort(others, comp); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 565 | |
Ben Kwa | 85c8cdd | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 566 | addAll(libraries); |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 567 | // Only add the spacer if it is actually separating something. |
| 568 | if (!libraries.isEmpty() && !others.isEmpty()) { |
| 569 | add(new SpacerItem()); |
| 570 | } |
Daichi Hirono | 9fb3389 | 2015-10-23 08:36:10 +0900 | [diff] [blame] | 571 | addAll(others); |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 572 | |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 573 | // Include apps that can handle this intent too. |
| 574 | if (handlerAppIntent != null) { |
| 575 | includeHandlerApps(context, handlerAppIntent); |
| 576 | } |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 577 | |
| 578 | mDragListener = dragListener; |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 579 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 580 | |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 581 | /** |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 582 | * Adds apps capable of handling the original intent will be included in list of roots (in |
| 583 | * special section at bottom). |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 584 | */ |
| 585 | private void includeHandlerApps(Context context, Intent handlerAppIntent) { |
| 586 | final PackageManager pm = context.getPackageManager(); |
| 587 | final List<ResolveInfo> infos = pm.queryIntentActivities( |
| 588 | handlerAppIntent, PackageManager.MATCH_DEFAULT_ONLY); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 589 | |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 590 | final List<AppItem> apps = new ArrayList<>(); |
| 591 | |
| 592 | // Omit ourselves from the list |
| 593 | for (ResolveInfo info : infos) { |
| 594 | if (!context.getPackageName().equals(info.activityInfo.packageName)) { |
| 595 | apps.add(new AppItem(info)); |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 596 | } |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 597 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 598 | |
Steve McKay | 323dffb | 2016-02-17 18:25:47 -0800 | [diff] [blame] | 599 | if (apps.size() > 0) { |
| 600 | add(new SpacerItem()); |
| 601 | addAll(apps); |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 602 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 603 | } |
Jeff Sharkey | 1d890e0 | 2013-08-15 11:24:03 -0700 | [diff] [blame] | 604 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 605 | @Override |
Garfield, Tan | 57facaf7 | 2016-05-27 15:02:35 -0700 | [diff] [blame] | 606 | public boolean hasStableIds() { |
| 607 | return true; |
| 608 | } |
| 609 | |
| 610 | @Override |
| 611 | public long getItemId(int position) { |
| 612 | // Ensure this method is only called in main thread because we don't have any |
| 613 | // concurrency protection. |
| 614 | assert(Looper.myLooper() == Looper.getMainLooper()); |
| 615 | |
| 616 | String stringId = getItem(position).mStringId; |
| 617 | |
| 618 | long id; |
| 619 | if (sIdMap.containsKey(stringId)) { |
| 620 | id = sIdMap.get(stringId); |
| 621 | } else { |
| 622 | id = sNextAvailableId++; |
| 623 | sIdMap.put(stringId, id); |
| 624 | } |
| 625 | |
| 626 | return id; |
| 627 | } |
| 628 | |
| 629 | @Override |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 630 | public View getView(int position, View convertView, ViewGroup parent) { |
| 631 | final Item item = getItem(position); |
Garfield, Tan | 804133e | 2016-04-20 15:13:56 -0700 | [diff] [blame] | 632 | final View view = item.getView(convertView, parent); |
| 633 | |
| 634 | if (item.isDropTarget()) { |
| 635 | view.setTag(R.id.item_position_tag, position); |
| 636 | view.setOnDragListener(mDragListener); |
| 637 | } else { |
| 638 | view.setTag(R.id.item_position_tag, null); |
| 639 | view.setOnDragListener(null); |
| 640 | } |
| 641 | return view; |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 642 | } |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 643 | |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 644 | @Override |
| 645 | public boolean areAllItemsEnabled() { |
| 646 | return false; |
| 647 | } |
| 648 | |
| 649 | @Override |
| 650 | public boolean isEnabled(int position) { |
| 651 | return getItemViewType(position) != 1; |
| 652 | } |
| 653 | |
| 654 | @Override |
| 655 | public int getItemViewType(int position) { |
| 656 | final Item item = getItem(position); |
| 657 | if (item instanceof RootItem || item instanceof AppItem) { |
| 658 | return 0; |
| 659 | } else { |
| 660 | return 1; |
Jeff Sharkey | 4045780 | 2013-09-21 13:57:33 -0700 | [diff] [blame] | 661 | } |
Jeff Sharkey | 9dd0262 | 2013-09-27 16:44:11 -0700 | [diff] [blame] | 662 | } |
| 663 | |
| 664 | @Override |
| 665 | public int getViewTypeCount() { |
| 666 | return 2; |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 667 | } |
| 668 | } |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 669 | |
Ben Kwa | 85c8cdd | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 670 | public static class RootComparator implements Comparator<RootItem> { |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 671 | @Override |
Ben Kwa | 85c8cdd | 2015-10-09 07:48:00 -0700 | [diff] [blame] | 672 | public int compare(RootItem lhs, RootItem rhs) { |
Steve McKay | 55c00e7 | 2016-02-18 15:32:16 -0800 | [diff] [blame] | 673 | return lhs.root.compareTo(rhs.root); |
Jeff Sharkey | b6a7f2c | 2013-08-27 18:26:48 -0700 | [diff] [blame] | 674 | } |
| 675 | } |
Jeff Sharkey | 7611221 | 2013-08-06 11:26:10 -0700 | [diff] [blame] | 676 | } |