blob: 2980e2338a33d66c0c47b1f034e7e600ee97ffae [file] [log] [blame]
Jeff Sharkey54e55b72013-06-30 20:02:59 -07001/*
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
17package com.android.documentsui;
18
19import android.app.Fragment;
20import android.app.FragmentManager;
21import android.app.FragmentTransaction;
22import android.content.Context;
23import android.content.pm.PackageManager;
24import android.content.pm.ProviderInfo;
25import android.os.Bundle;
26import android.provider.DocumentsContract;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.ViewGroup;
30import android.widget.AdapterView;
31import android.widget.AdapterView.OnItemClickListener;
32import android.widget.ArrayAdapter;
33import android.widget.GridView;
34import android.widget.ImageView;
35import android.widget.TextView;
36
37import com.google.android.collect.Lists;
38
39import java.util.List;
40
41/**
42 * Display all known storage backends.
43 */
44public class BackendFragment extends Fragment {
45
46 // TODO: handle multiple accounts from single backend
47
48 private GridView mGridView;
49 private BackendAdapter mAdapter;
50
51 public static void show(FragmentManager fm) {
52 final BackendFragment fragment = new BackendFragment();
53
54 final FragmentTransaction ft = fm.beginTransaction();
55 ft.replace(R.id.directory, fragment);
56 ft.setBreadCrumbTitle("TOP");
57 ft.commitAllowingStateLoss();
58 }
59
60 @Override
61 public View onCreateView(
62 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
63 final Context context = inflater.getContext();
64
65 // Gather known storage providers
66 final List<ProviderInfo> providers = context.getPackageManager()
67 .queryContentProviders(null, -1, PackageManager.GET_META_DATA);
68 final List<ProviderInfo> backends = Lists.newArrayList();
69 for (ProviderInfo info : providers) {
70 if (info.metaData != null
71 && info.metaData.containsKey(DocumentsContract.META_DATA_DOCUMENT_PROVIDER)) {
72 backends.add(info);
73 }
74 }
75
76 final View view = inflater.inflate(R.layout.fragment_backend, container, false);
77
78 mGridView = (GridView) view.findViewById(R.id.grid);
79 mGridView.setOnItemClickListener(mItemListener);
80
81 mAdapter = new BackendAdapter(context, backends);
82 mGridView.setAdapter(mAdapter);
83 mGridView.setNumColumns(GridView.AUTO_FIT);
84
85 return view;
86 }
87
88 private OnItemClickListener mItemListener = new OnItemClickListener() {
89 @Override
90 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
91 final ProviderInfo info = mAdapter.getItem(position);
92 ((DocumentsActivity) getActivity()).onBackendPicked(info);
93 }
94 };
95
96 public static class BackendAdapter extends ArrayAdapter<ProviderInfo> {
97 public BackendAdapter(Context context, List<ProviderInfo> list) {
98 super(context, android.R.layout.simple_list_item_1, list);
99 }
100
101 @Override
102 public View getView(int position, View convertView, ViewGroup parent) {
103 if (convertView == null) {
104 convertView = LayoutInflater.from(parent.getContext())
105 .inflate(R.layout.item_backend, parent, false);
106 }
107
108 final ImageView icon = (ImageView) convertView.findViewById(android.R.id.icon);
109 final TextView text1 = (TextView) convertView.findViewById(android.R.id.text1);
110
111 final PackageManager pm = parent.getContext().getPackageManager();
112 final ProviderInfo info = getItem(position);
113 icon.setImageDrawable(info.loadIcon(pm));
114 text1.setText(info.loadLabel(pm));
115
116 return convertView;
117 }
118 }
119}