Jeff Sharkey | 54e55b7 | 2013-06-30 20:02:59 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.documentsui; |
| 18 | |
| 19 | import android.app.Fragment; |
| 20 | import android.app.FragmentManager; |
| 21 | import android.app.FragmentTransaction; |
| 22 | import android.content.Context; |
| 23 | import android.content.pm.PackageManager; |
| 24 | import android.content.pm.ProviderInfo; |
| 25 | import android.os.Bundle; |
| 26 | import android.provider.DocumentsContract; |
| 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
| 29 | import android.view.ViewGroup; |
| 30 | import android.widget.AdapterView; |
| 31 | import android.widget.AdapterView.OnItemClickListener; |
| 32 | import android.widget.ArrayAdapter; |
| 33 | import android.widget.GridView; |
| 34 | import android.widget.ImageView; |
| 35 | import android.widget.TextView; |
| 36 | |
| 37 | import com.google.android.collect.Lists; |
| 38 | |
| 39 | import java.util.List; |
| 40 | |
| 41 | /** |
| 42 | * Display all known storage backends. |
| 43 | */ |
| 44 | public 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 | } |