RootsFragment.java revision 54ca29a5b94c2edf461c5433825d4ae17469fd7c
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;
2966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.provider.DocumentsContract;
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;
4366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport com.android.documentsui.model.Root;
4466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport com.android.documentsui.model.Root.RootComparator;
4566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
4666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport java.util.Collection;
4754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport java.util.Iterator;
4854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport java.util.List;
4966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
5066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey/**
5166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey * Display list of known storage backend roots.
5266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey */
5366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeypublic class RootsFragment extends Fragment {
5466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
5566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    private ListView mList;
5666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    private SectionedRootsAdapter mAdapter;
5766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
5854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static final String EXTRA_INCLUDE_APPS = "includeApps";
5954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
6054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    public static void show(FragmentManager fm, Intent includeApps) {
6154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        final Bundle args = new Bundle();
6254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        args.putParcelable(EXTRA_INCLUDE_APPS, includeApps);
6354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
6466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final RootsFragment fragment = new RootsFragment();
6554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        fragment.setArguments(args);
6666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
6766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final FragmentTransaction ft = fm.beginTransaction();
6866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        ft.replace(R.id.container_roots, fragment);
6966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        ft.commitAllowingStateLoss();
7066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
7166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
7266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    public static RootsFragment get(FragmentManager fm) {
7366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        return (RootsFragment) fm.findFragmentById(R.id.container_roots);
7466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
7566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
7666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    @Override
7766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    public View onCreateView(
7866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
7966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final Context context = inflater.getContext();
8066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
8166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final View view = inflater.inflate(R.layout.fragment_roots, container, false);
8266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        mList = (ListView) view.findViewById(android.R.id.list);
8366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        mList.setOnItemClickListener(mItemListener);
8466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
8554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        final Intent includeApps = getArguments().getParcelable(EXTRA_INCLUDE_APPS);
8654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        mAdapter = new SectionedRootsAdapter(context, RootsCache.getRoots(context), includeApps);
8754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
8866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        return view;
8966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
9066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
919fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    @Override
929fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    public void onStart() {
939fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey        super.onStart();
949fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
959fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey        final Context context = getActivity();
9654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        mAdapter.updateVisible(SettingsActivity.getDisplayAdvancedDevices(context));
9754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        mList.setAdapter(mAdapter);
989fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    }
999fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
10066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    private OnItemClickListener mItemListener = new OnItemClickListener() {
10166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
10266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
10354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final DocumentsActivity activity = DocumentsActivity.get(RootsFragment.this);
10454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final Object item = mAdapter.getItem(position);
10554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (item instanceof Root) {
10654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                activity.onRootPicked((Root) item, true);
10754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            } else if (item instanceof ResolveInfo) {
10854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                activity.onAppPicked((ResolveInfo) item);
10954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            } else {
11054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                throw new IllegalStateException("Unknown root: " + item);
11154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
11266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
11366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    };
11466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
11554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static class RootsAdapter extends ArrayAdapter<Root> implements SectionAdapter {
11666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private int mHeaderId;
11766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
11866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public RootsAdapter(Context context, int headerId) {
11966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            super(context, 0);
12066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mHeaderId = headerId;
12166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
12266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
12366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
12466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public View getView(int position, View convertView, ViewGroup parent) {
1259fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            final Context context = parent.getContext();
12666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (convertView == null) {
1279fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                convertView = LayoutInflater.from(context)
12866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        .inflate(R.layout.item_root, parent, false);
12966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
13066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
13166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
13266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
13366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
13466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
13566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final Root root = getItem(position);
13666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            icon.setImageDrawable(root.icon);
13766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            title.setText(root.title);
13866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
1399fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            // Device summary is always available space
1409fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            final String summaryText;
1419fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            if ((root.rootType == DocumentsContract.ROOT_TYPE_DEVICE
1429fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                    || root.rootType == DocumentsContract.ROOT_TYPE_DEVICE_ADVANCED)
1439fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                    && root.availableBytes >= 0) {
1449fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                summaryText = context.getString(R.string.root_available_bytes,
1459fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                        Formatter.formatFileSize(context, root.availableBytes));
1469fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            } else {
1479fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                summaryText = root.summary;
1489fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            }
1499fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
1509fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            summary.setText(summaryText);
1519fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            summary.setVisibility(summaryText != null ? View.VISIBLE : View.GONE);
15266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
15366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            return convertView;
15466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
15566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
15666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
15766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public View getHeaderView(View convertView, ViewGroup parent) {
15866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (convertView == null) {
15966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                convertView = LayoutInflater.from(parent.getContext())
16066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        .inflate(R.layout.item_root_header, parent, false);
16166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
16266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
16366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
16466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            title.setText(mHeaderId);
16566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
16666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            return convertView;
16766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
16866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
16966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
17054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static class AppsAdapter extends ArrayAdapter<ResolveInfo> implements SectionAdapter {
17154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public AppsAdapter(Context context) {
17254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            super(context, 0);
17354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
17454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
17554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        @Override
17654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public View getView(int position, View convertView, ViewGroup parent) {
17754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final Context context = parent.getContext();
17854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final PackageManager pm = context.getPackageManager();
17954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (convertView == null) {
18054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                convertView = LayoutInflater.from(context)
18154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        .inflate(R.layout.item_root, parent, false);
18254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
18354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
18454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
18554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
18654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
18754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
18854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final ResolveInfo info = getItem(position);
18954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            icon.setImageDrawable(info.loadIcon(pm));
19054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            title.setText(info.loadLabel(pm));
19154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
19254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            // TODO: match existing summary behavior from disambig dialog
19354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            summary.setVisibility(View.GONE);
19454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
19554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            return convertView;
19654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
19754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
19854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        @Override
19954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public View getHeaderView(View convertView, ViewGroup parent) {
20054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (convertView == null) {
20154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                convertView = LayoutInflater.from(parent.getContext())
20254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        .inflate(R.layout.item_root_header, parent, false);
20354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
20454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
20554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
20654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            title.setText(R.string.root_type_apps);
20754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
20854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            return convertView;
20954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
21054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    }
21154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
21254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static class SectionedRootsAdapter extends SectionedListAdapter {
21366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mServices;
21466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mShortcuts;
21566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mDevices;
21666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mDevicesAdvanced;
21754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        private final AppsAdapter mApps;
21866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
21954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public SectionedRootsAdapter(Context context, Collection<Root> roots, Intent includeApps) {
22066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mServices = new RootsAdapter(context, R.string.root_type_service);
22166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mShortcuts = new RootsAdapter(context, R.string.root_type_shortcut);
22266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevices = new RootsAdapter(context, R.string.root_type_device);
22366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevicesAdvanced = new RootsAdapter(context, R.string.root_type_device);
22454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            mApps = new AppsAdapter(context);
22566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
22666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            for (Root root : roots) {
22766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                Log.d(TAG, "Found rootType=" + root.rootType);
22866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                switch (root.rootType) {
22966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                    case DocumentsContract.ROOT_TYPE_SERVICE:
23066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mServices.add(root);
23166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
23266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                    case DocumentsContract.ROOT_TYPE_SHORTCUT:
23366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mShortcuts.add(root);
23466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
23566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                    case DocumentsContract.ROOT_TYPE_DEVICE:
23666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mDevices.add(root);
23766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mDevicesAdvanced.add(root);
23866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
23966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                    case DocumentsContract.ROOT_TYPE_DEVICE_ADVANCED:
24066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mDevicesAdvanced.add(root);
24166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
24266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                }
24366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
24466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
24554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (includeApps != null) {
24654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                final PackageManager pm = context.getPackageManager();
24754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                final List<ResolveInfo> infos = pm.queryIntentActivities(
24854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        includeApps, PackageManager.MATCH_DEFAULT_ONLY);
24954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
25054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                // Omit ourselves from the list
25154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                for (ResolveInfo info : infos) {
25254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                    if (!context.getPackageName().equals(info.activityInfo.packageName)) {
25354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        mApps.add(info);
25454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                    }
25554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                }
25654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
25754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
25866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final RootComparator comp = new RootComparator();
25966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mServices.sort(comp);
26066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mShortcuts.sort(comp);
26166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevices.sort(comp);
26266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevicesAdvanced.sort(comp);
26366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
26466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
26554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public void updateVisible(boolean showAdvanced) {
26666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            clearSections();
26766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (mServices.getCount() > 0) {
26866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                addSection(mServices);
26966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
27066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (mShortcuts.getCount() > 0) {
27166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                addSection(mShortcuts);
27266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
27366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
27466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final RootsAdapter devices = showAdvanced ? mDevicesAdvanced : mDevices;
27566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (devices.getCount() > 0) {
27666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                addSection(devices);
27766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
27854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
27954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (mApps.getCount() > 0) {
28054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                addSection(mApps);
28154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
28266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
28366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
28466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey}
285