RootsFragment.java revision 28c05ee8931cecf4c51c470e0043d30196010c49
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 android.app.Fragment;
2066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.app.FragmentManager;
2166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.app.FragmentTransaction;
2266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.content.Context;
2354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport android.content.Intent;
2454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport android.content.pm.PackageManager;
2554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkeyimport android.content.pm.ResolveInfo;
2666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.os.Bundle;
27ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkeyimport android.provider.DocumentsContract.Root;
289fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkeyimport android.text.format.Formatter;
2966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.view.LayoutInflater;
3066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.view.View;
3166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.view.ViewGroup;
3266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.AdapterView;
3366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.AdapterView.OnItemClickListener;
3466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.ArrayAdapter;
3566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.ImageView;
3666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.ListView;
3766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport android.widget.TextView;
3866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
39348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkeyimport com.android.documentsui.DocumentsActivity.State;
4066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkeyimport com.android.documentsui.SectionedListAdapter.SectionAdapter;
41ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkeyimport com.android.documentsui.model.DocumentInfo;
42ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkeyimport com.android.documentsui.model.RootInfo;
4328c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkeyimport com.android.internal.util.Objects;
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();
7866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
7966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        final View view = inflater.inflate(R.layout.fragment_roots, container, false);
8066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        mList = (ListView) view.findViewById(android.R.id.list);
8166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        mList.setOnItemClickListener(mItemListener);
8228c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey        mList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
8366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
8466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        return view;
8566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
8666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
879fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    @Override
889fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    public void onStart() {
899fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey        super.onStart();
90348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey        updateRootsAdapter();
91348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey    }
929fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
93348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey    private void updateRootsAdapter() {
949fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey        final Context context = getActivity();
95348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey
96348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey        final State state = ((DocumentsActivity) context).getDisplayState();
97348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey        state.showAdvanced = SettingsActivity.getDisplayAdvancedDevices(context);
98348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey
99348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey        final RootsCache roots = DocumentsApplication.getRootsCache(context);
100348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey        final List<RootInfo> matchingRoots = roots.getMatchingRoots(state);
101348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey        final Intent includeApps = getArguments().getParcelable(EXTRA_INCLUDE_APPS);
102348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey
103348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey        mAdapter = new SectionedRootsAdapter(context, matchingRoots, includeApps);
10454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        mList.setAdapter(mAdapter);
10528c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
10628c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey        onCurrentRootChanged();
10728c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey    }
10828c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
10928c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey    public void onCurrentRootChanged() {
11028c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey        if (mAdapter == null) return;
11128c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey
11228c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey        final RootInfo root = ((DocumentsActivity) getActivity()).getCurrentRoot();
11328c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey        for (int i = 0; i < mAdapter.getCount(); i++) {
11428c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey            final Object item = mAdapter.getItem(i);
11528c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey            if (Objects.equal(item, root)) {
11628c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey                mList.setItemChecked(i, true);
11728c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey                return;
11828c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey            }
11928c05ee8931cecf4c51c470e0043d30196010c49Jeff Sharkey        }
1209fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey    }
1219fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
12266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    private OnItemClickListener mItemListener = new OnItemClickListener() {
12366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
12466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
12554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final DocumentsActivity activity = DocumentsActivity.get(RootsFragment.this);
12654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final Object item = mAdapter.getItem(position);
127ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey            if (item instanceof RootInfo) {
128ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey                activity.onRootPicked((RootInfo) item, true);
12954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            } else if (item instanceof ResolveInfo) {
13054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                activity.onAppPicked((ResolveInfo) item);
13154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            } else {
13254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                throw new IllegalStateException("Unknown root: " + item);
13354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
13466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
13566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    };
13666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
137ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey    private static class RootsAdapter extends ArrayAdapter<RootInfo> implements SectionAdapter {
13866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private int mHeaderId;
13966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
14066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public RootsAdapter(Context context, int headerId) {
14166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            super(context, 0);
14266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mHeaderId = headerId;
14366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
14466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
14566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
14666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public View getView(int position, View convertView, ViewGroup parent) {
1479fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            final Context context = parent.getContext();
14866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (convertView == null) {
1499fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                convertView = LayoutInflater.from(context)
15066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        .inflate(R.layout.item_root, parent, false);
15166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
15266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
15366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
15466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
15566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
15666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
157ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey            final RootInfo root = getItem(position);
158aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            icon.setImageDrawable(root.loadIcon(context));
15966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            title.setText(root.title);
16066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
1619fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            // Device summary is always available space
1629fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            final String summaryText;
163ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey            if (root.rootType == Root.ROOT_TYPE_DEVICE && root.availableBytes >= 0) {
1649fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                summaryText = context.getString(R.string.root_available_bytes,
1659fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                        Formatter.formatFileSize(context, root.availableBytes));
1669fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            } else {
1679fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey                summaryText = root.summary;
1689fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            }
1699fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey
1709fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            summary.setText(summaryText);
1719fb567b59112f99e64e0bff6f343188331bad28dJeff Sharkey            summary.setVisibility(summaryText != null ? View.VISIBLE : View.GONE);
17266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
17366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            return convertView;
17466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
17566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
17666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        @Override
17766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        public View getHeaderView(View convertView, ViewGroup parent) {
17866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (convertView == null) {
17966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                convertView = LayoutInflater.from(parent.getContext())
18066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        .inflate(R.layout.item_root_header, parent, false);
18166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
18266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
18366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
18466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            title.setText(mHeaderId);
18566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
18666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            return convertView;
18766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
18866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
18966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
19054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static class AppsAdapter extends ArrayAdapter<ResolveInfo> implements SectionAdapter {
19154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public AppsAdapter(Context context) {
19254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            super(context, 0);
19354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
19454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
19554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        @Override
19654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public View getView(int position, View convertView, ViewGroup parent) {
19754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final Context context = parent.getContext();
19854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final PackageManager pm = context.getPackageManager();
19954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (convertView == null) {
20054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                convertView = LayoutInflater.from(context)
20154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        .inflate(R.layout.item_root, parent, false);
20254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
20354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
20454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
20554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
20654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);
20754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
20854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final ResolveInfo info = getItem(position);
20954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            icon.setImageDrawable(info.loadIcon(pm));
21054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            title.setText(info.loadLabel(pm));
21154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
21254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            // TODO: match existing summary behavior from disambig dialog
21354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            summary.setVisibility(View.GONE);
21454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
21554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            return convertView;
21654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
21754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
21854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        @Override
21954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        public View getHeaderView(View convertView, ViewGroup parent) {
22054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (convertView == null) {
22154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                convertView = LayoutInflater.from(parent.getContext())
22254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        .inflate(R.layout.item_root_header, parent, false);
22354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
22454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
22554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
22654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            title.setText(R.string.root_type_apps);
22754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
22854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            return convertView;
22954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        }
23054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    }
23154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
23254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey    private static class SectionedRootsAdapter extends SectionedListAdapter {
23366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mServices;
23466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mShortcuts;
23566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        private final RootsAdapter mDevices;
23654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey        private final AppsAdapter mApps;
23766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
238ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey        public SectionedRootsAdapter(Context context, List<RootInfo> roots, Intent includeApps) {
23966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mServices = new RootsAdapter(context, R.string.root_type_service);
24066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mShortcuts = new RootsAdapter(context, R.string.root_type_shortcut);
24166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevices = new RootsAdapter(context, R.string.root_type_device);
24254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            mApps = new AppsAdapter(context);
24366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
244ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey            for (RootInfo root : roots) {
24566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                switch (root.rootType) {
246ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey                    case Root.ROOT_TYPE_SERVICE:
24766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mServices.add(root);
24866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
249ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey                    case Root.ROOT_TYPE_SHORTCUT:
25066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        mShortcuts.add(root);
25166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
252ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey                    case Root.ROOT_TYPE_DEVICE:
253348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey                        mDevices.add(root);
25466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                        break;
25566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                }
25666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
25766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
25854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (includeApps != null) {
25954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                final PackageManager pm = context.getPackageManager();
26054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                final List<ResolveInfo> infos = pm.queryIntentActivities(
26154ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        includeApps, PackageManager.MATCH_DEFAULT_ONLY);
26254ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
26354ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                // Omit ourselves from the list
26454ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                for (ResolveInfo info : infos) {
26554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                    if (!context.getPackageName().equals(info.activityInfo.packageName)) {
26654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                        mApps.add(info);
26754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                    }
26854ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                }
26954ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
27054ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey
27166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            final RootComparator comp = new RootComparator();
27266516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mServices.sort(comp);
27366516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mShortcuts.sort(comp);
27466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            mDevices.sort(comp);
27566516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey
27666516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (mServices.getCount() > 0) {
27766516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                addSection(mServices);
27866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
27966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            if (mShortcuts.getCount() > 0) {
28066516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey                addSection(mShortcuts);
28166516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
282348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey            if (mDevices.getCount() > 0) {
283348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey                addSection(mDevices);
28466516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey            }
28554ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            if (mApps.getCount() > 0) {
28654ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey                addSection(mApps);
28754ca29a5b94c2edf461c5433825d4ae17469fd7cJeff Sharkey            }
28866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey        }
28966516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey    }
290aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey
291ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey    public static class RootComparator implements Comparator<RootInfo> {
292aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey        @Override
293ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey        public int compare(RootInfo lhs, RootInfo rhs) {
294348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey            if (lhs.authority == null) {
295348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey                return -1;
296348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey            } else if (rhs.authority == null) {
297348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey                return 1;
298348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey            }
299348ad6866b91afa4d59d45df533ef88094c74d13Jeff Sharkey
300ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey            final int score = DocumentInfo.compareToIgnoreCaseNullable(lhs.title, rhs.title);
301aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            if (score != 0) {
302aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey                return score;
303aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            } else {
304ae9b51bfa313c51a31af30875a71255d7b6d2e61Jeff Sharkey                return DocumentInfo.compareToIgnoreCaseNullable(lhs.summary, rhs.summary);
305aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey            }
306aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey        }
307aeb16e2435f9975b9fa1fc4b747796647a21292eJeff Sharkey    }
30866516697a29cf00d93893a1011d3befc7c1ee37fJeff Sharkey}
309