RootsFragment.java revision aeb16e2435f9975b9fa1fc4b747796647a21292e
166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey/*
266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * Copyright (C) 2013 The Android Open Source Project
366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey *
466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * Licensed under the Apache License, Version 2.0 (the "License");
566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * you may not use this file except in compliance with the License.
666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * You may obtain a copy of the License at
766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey *
866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey *      http://www.apache.org/licenses/LICENSE-2.0
966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey *
1066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * Unless required by applicable law or agreed to in writing, software
1166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * distributed under the License is distributed on an "AS IS" BASIS,
1266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * See the License for the specific language governing permissions and
1466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * limitations under the License.
1566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey */
1666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
1766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeypackage com.android.documentsui;
1866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
1966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport static com.android.documentsui.DocumentsActivity.TAG;
2066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
2166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.app.Fragment;
2266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.app.FragmentManager;
2366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.app.FragmentTransaction;
2466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.content.Context;
2554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport android.content.Intent;
2654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport android.content.pm.PackageManager;
2754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport android.content.pm.ResolveInfo;
2866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.os.Bundle;
29aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkeyimport android.provider.DocumentsContract.DocumentRoot;
309fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkeyimport android.text.format.Formatter;
3166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.util.Log;
3266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.view.LayoutInflater;
3366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.view.View;
3466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.view.ViewGroup;
3566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.AdapterView;
3666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.AdapterView.OnItemClickListener;
3766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.ArrayAdapter;
3866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.ImageView;
3966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.ListView;
4066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.TextView;
4166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
4266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport com.android.documentsui.SectionedListAdapter.SectionAdapter;
43aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkeyimport com.android.documentsui.model.Document;
4466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
45aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkeyimport java.util.Comparator;
4654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport java.util.List;
4766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
4866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey/**
4966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * Display list of known storage backend roots.
5066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey */
5166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeypublic class RootsFragment extends Fragment {
5266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
5366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    private ListView mList;
5466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    private SectionedRootsAdapter mAdapter;
5566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
5654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static final String EXTRA_INCLUDE_APPS = "includeApps";
5754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
5854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    public static void show(FragmentManager fm, Intent includeApps) {
5954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        final Bundle args = new Bundle();
6054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        args.putParcelable(EXTRA_INCLUDE_APPS, includeApps);
6154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
6266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final RootsFragment fragment = new RootsFragment();
6354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        fragment.setArguments(args);
6466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
6566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final FragmentTransaction ft = fm.beginTransaction();
6666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        ft.replace(R.id.container_roots, fragment);
6766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        ft.commitAllowingStateLoss();
6866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
6966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
7066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    public static RootsFragment get(FragmentManager fm) {
7166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        return (RootsFragment) fm.findFragmentById(R.id.container_roots);
7266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
7366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
7466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    @Override
7566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    public View onCreateView(
7666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
7766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final Context context = inflater.getContext();
784eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        final RootsCache roots = DocumentsApplication.getRootsCache(context);
7966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
8066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final View view = inflater.inflate(R.layout.fragment_roots, container, false);
8166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        mList = (ListView) view.findViewById(android.R.id.list);
8266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        mList.setOnItemClickListener(mItemListener);
8366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
8454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        final Intent includeApps = getArguments().getParcelable(EXTRA_INCLUDE_APPS);
854eb407a832b7d6a2d62a535e5cab70b00a0bc8edJeff Sharkey        mAdapter = new SectionedRootsAdapter(context, roots.getRoots(), includeApps);
8654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
8766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        return view;
8866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
8966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
909fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    @Override
919fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    public void onStart() {
929fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey        super.onStart();
939fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
949fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey        final Context context = getActivity();
9554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        mAdapter.updateVisible(SettingsActivity.getDisplayAdvancedDevices(context));
9654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        mList.setAdapter(mAdapter);
979fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    }
989fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
9966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    private OnItemClickListener mItemListener = new OnItemClickListener() {
10066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
10166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
10254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final DocumentsActivity activity = DocumentsActivity.get(RootsFragment.this);
10354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final Object item = mAdapter.getItem(position);
104aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            if (item instanceof DocumentRoot) {
105aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                activity.onRootPicked((DocumentRoot) item, true);
10654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            } else if (item instanceof ResolveInfo) {
10754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                activity.onAppPicked((ResolveInfo) item);
10854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            } else {
10954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                throw new IllegalStateException("Unknown root: " + item);
11054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
11166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
11266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    };
11366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
114aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey    private static class RootsAdapter extends ArrayAdapter<DocumentRoot> implements SectionAdapter {
11566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private int mHeaderId;
11666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
11766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public RootsAdapter(Context context, int headerId) {
11866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            super(context, 0);
11966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mHeaderId = headerId;
12066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
12166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
12266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
12366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public View getView(int position, View convertView, ViewGroup parent) {
1249fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            final Context context = parent.getContext();
12566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (convertView == null) {
1269fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                convertView = LayoutInflater.from(context)
12766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        .inflate(R.layout.item_root, parent, false);
12866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
12966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
13066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
13166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
13266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
13366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
134aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            final DocumentRoot root = getItem(position);
135aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            icon.setImageDrawable(root.loadIcon(context));
13666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            title.setText(root.title);
13766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
1389fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            // Device summary is always available space
1399fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            final String summaryText;
140aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            if ((root.rootType == DocumentRoot.ROOT_TYPE_DEVICE
141aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                    || root.rootType == DocumentRoot.ROOT_TYPE_DEVICE_ADVANCED)
1429fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                    && root.availableBytes >= 0) {
1439fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                summaryText = context.getString(R.string.root_available_bytes,
1449fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                        Formatter.formatFileSize(context, root.availableBytes));
1459fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            } else {
1469fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                summaryText = root.summary;
1479fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            }
1489fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
1499fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            summary.setText(summaryText);
1509fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            summary.setVisibility(summaryText != null ? View.VISIBLE : View.GONE);
15166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
15266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            return convertView;
15366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
15466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
15566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
15666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public View getHeaderView(View convertView, ViewGroup parent) {
15766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (convertView == null) {
15866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                convertView = LayoutInflater.from(parent.getContext())
15966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        .inflate(R.layout.item_root_header, parent, false);
16066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
16166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
16266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
16366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            title.setText(mHeaderId);
16466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
16566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            return convertView;
16666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
16766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
16866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
16954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static class AppsAdapter extends ArrayAdapter<ResolveInfo> implements SectionAdapter {
17054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public AppsAdapter(Context context) {
17154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            super(context, 0);
17254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
17354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
17454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        @Override
17554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public View getView(int position, View convertView, ViewGroup parent) {
17654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final Context context = parent.getContext();
17754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final PackageManager pm = context.getPackageManager();
17854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (convertView == null) {
17954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                convertView = LayoutInflater.from(context)
18054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        .inflate(R.layout.item_root, parent, false);
18154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
18254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
18354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
18454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
18554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
18654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
18754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final ResolveInfo info = getItem(position);
18854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            icon.setImageDrawable(info.loadIcon(pm));
18954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            title.setText(info.loadLabel(pm));
19054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
19154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            // TODO: match existing summary behavior from disambig dialog
19254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            summary.setVisibility(View.GONE);
19354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
19454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            return convertView;
19554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
19654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
19754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        @Override
19854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public View getHeaderView(View convertView, ViewGroup parent) {
19954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (convertView == null) {
20054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                convertView = LayoutInflater.from(parent.getContext())
20154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        .inflate(R.layout.item_root_header, parent, false);
20254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
20354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
20454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
20554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            title.setText(R.string.root_type_apps);
20654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
20754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            return convertView;
20854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
20954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    }
21054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
21154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static class SectionedRootsAdapter extends SectionedListAdapter {
21266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mServices;
21366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mShortcuts;
21466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mDevices;
21566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mDevicesAdvanced;
21654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        private final AppsAdapter mApps;
21766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
218aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey        public SectionedRootsAdapter(Context context, List<DocumentRoot> roots, Intent includeApps) {
21966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mServices = new RootsAdapter(context, R.string.root_type_service);
22066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mShortcuts = new RootsAdapter(context, R.string.root_type_shortcut);
22166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevices = new RootsAdapter(context, R.string.root_type_device);
22266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevicesAdvanced = new RootsAdapter(context, R.string.root_type_device);
22354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            mApps = new AppsAdapter(context);
22466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
225aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            for (DocumentRoot root : roots) {
22666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                Log.d(TAG, "Found rootType=" + root.rootType);
22766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                switch (root.rootType) {
228aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                    case DocumentRoot.ROOT_TYPE_SERVICE:
22966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mServices.add(root);
23066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
231aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                    case DocumentRoot.ROOT_TYPE_SHORTCUT:
23266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mShortcuts.add(root);
23366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
234aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                    case DocumentRoot.ROOT_TYPE_DEVICE:
23566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mDevices.add(root);
23666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mDevicesAdvanced.add(root);
23766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
238aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                    case DocumentRoot.ROOT_TYPE_DEVICE_ADVANCED:
23966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mDevicesAdvanced.add(root);
24066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
24166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                }
24266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
24366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
24454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (includeApps != null) {
24554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                final PackageManager pm = context.getPackageManager();
24654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                final List<ResolveInfo> infos = pm.queryIntentActivities(
24754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        includeApps, PackageManager.MATCH_DEFAULT_ONLY);
24854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
24954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                // Omit ourselves from the list
25054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                for (ResolveInfo info : infos) {
25154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                    if (!context.getPackageName().equals(info.activityInfo.packageName)) {
25254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        mApps.add(info);
25354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                    }
25454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                }
25554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
25654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
25766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final RootComparator comp = new RootComparator();
25866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mServices.sort(comp);
25966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mShortcuts.sort(comp);
26066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevices.sort(comp);
26166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevicesAdvanced.sort(comp);
26266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
26366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
26454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public void updateVisible(boolean showAdvanced) {
26566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            clearSections();
26666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (mServices.getCount() > 0) {
26766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                addSection(mServices);
26866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
26966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (mShortcuts.getCount() > 0) {
27066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                addSection(mShortcuts);
27166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
27266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
27366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final RootsAdapter devices = showAdvanced ? mDevicesAdvanced : mDevices;
27466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (devices.getCount() > 0) {
27566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                addSection(devices);
27666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
27754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
27854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (mApps.getCount() > 0) {
27954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                addSection(mApps);
28054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
28166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
28266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
283aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey
284aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey    public static class RootComparator implements Comparator<DocumentRoot> {
285aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey        @Override
286aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey        public int compare(DocumentRoot lhs, DocumentRoot rhs) {
287aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            final int score = Document.compareToIgnoreCaseNullable(lhs.title, rhs.title);
288aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            if (score != 0) {
289aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                return score;
290aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            } else {
291aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                return Document.compareToIgnoreCaseNullable(lhs.summary, rhs.summary);
292aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            }
293aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey        }
294aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey    }
29566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey}
296