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