summaryrefslogtreecommitdiffstats
path: root/packages/DocumentsUI/src/com/android/documentsui/RootsFragment.java
blob: c4e9c15aa6867ab4f9a86f3212fdea3566f88741 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.documentsui;

import static com.android.documentsui.DocumentsActivity.TAG;

import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.android.documentsui.SectionedListAdapter.SectionAdapter;
import com.android.documentsui.model.Root;
import com.android.documentsui.model.Root.RootComparator;

import java.util.Collection;

/**
 * Display list of known storage backend roots.
 */
public class RootsFragment extends Fragment {

    private ListView mList;
    private SectionedRootsAdapter mAdapter;

    public static void show(FragmentManager fm) {
        final RootsFragment fragment = new RootsFragment();

        final FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.container_roots, fragment);
        ft.commitAllowingStateLoss();
    }

    public static RootsFragment get(FragmentManager fm) {
        return (RootsFragment) fm.findFragmentById(R.id.container_roots);
    }

    @Override
    public View onCreateView(
            LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final Context context = inflater.getContext();

        final View view = inflater.inflate(R.layout.fragment_roots, container, false);
        mList = (ListView) view.findViewById(android.R.id.list);

        mAdapter = new SectionedRootsAdapter(context, RootsCache.getRoots(context));
        mList.setAdapter(mAdapter);
        mList.setOnItemClickListener(mItemListener);

        return view;
    }

    private OnItemClickListener mItemListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            final Root root = (Root) mAdapter.getItem(position);
            ((DocumentsActivity) getActivity()).onRootPicked(root, true);
        }
    };

    public static class RootsAdapter extends ArrayAdapter<Root> implements SectionAdapter {
        private int mHeaderId;

        public RootsAdapter(Context context, int headerId) {
            super(context, 0);
            mHeaderId = headerId;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_root, parent, false);
            }

            final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
            final TextView summary = (TextView) convertView.findViewById(android.R.id.summary);

            final Root root = getItem(position);
            icon.setImageDrawable(root.icon);
            title.setText(root.title);

            summary.setText(root.summary);
            summary.setVisibility(root.summary != null ? View.VISIBLE : View.GONE);

            return convertView;
        }

        @Override
        public View getHeaderView(View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_root_header, parent, false);
            }

            final TextView title = (TextView) convertView.findViewById(android.R.id.title);
            title.setText(mHeaderId);

            return convertView;
        }
    }

    public static class SectionedRootsAdapter extends SectionedListAdapter {
        private final RootsAdapter mServices;
        private final RootsAdapter mShortcuts;
        private final RootsAdapter mDevices;
        private final RootsAdapter mDevicesAdvanced;

        public SectionedRootsAdapter(Context context, Collection<Root> roots) {
            mServices = new RootsAdapter(context, R.string.root_type_service);
            mShortcuts = new RootsAdapter(context, R.string.root_type_shortcut);
            mDevices = new RootsAdapter(context, R.string.root_type_device);
            mDevicesAdvanced = new RootsAdapter(context, R.string.root_type_device);

            for (Root root : roots) {
                Log.d(TAG, "Found rootType=" + root.rootType);
                switch (root.rootType) {
                    case DocumentsContract.ROOT_TYPE_SERVICE:
                        mServices.add(root);
                        break;
                    case DocumentsContract.ROOT_TYPE_SHORTCUT:
                        mShortcuts.add(root);
                        break;
                    case DocumentsContract.ROOT_TYPE_DEVICE:
                        mDevices.add(root);
                        mDevicesAdvanced.add(root);
                        break;
                    case DocumentsContract.ROOT_TYPE_DEVICE_ADVANCED:
                        mDevicesAdvanced.add(root);
                        break;
                }
            }

            final RootComparator comp = new RootComparator();
            mServices.sort(comp);
            mShortcuts.sort(comp);
            mDevices.sort(comp);
            mDevicesAdvanced.sort(comp);

            // TODO: switch to hide advanced items by default
            setShowAdvanced(true);
        }

        public void setShowAdvanced(boolean showAdvanced) {
            clearSections();
            if (mServices.getCount() > 0) {
                addSection(mServices);
            }
            if (mShortcuts.getCount() > 0) {
                addSection(mShortcuts);
            }

            final RootsAdapter devices = showAdvanced ? mDevicesAdvanced : mDevices;
            if (devices.getCount() > 0) {
                addSection(devices);
            }
        }
    }
}