Jeff Sharkey | e22d02e | 2013-04-26 16:54:55 -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.Activity; |
| 20 | import android.app.FragmentManager; |
| 21 | import android.app.FragmentTransaction; |
| 22 | import android.app.ListFragment; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.pm.PackageManager; |
| 26 | import android.content.pm.ProviderInfo; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.provider.DocumentsContract; |
| 30 | import android.util.Log; |
| 31 | import android.view.LayoutInflater; |
| 32 | import android.view.View; |
| 33 | import android.view.ViewGroup; |
| 34 | import android.widget.ArrayAdapter; |
| 35 | import android.widget.ListView; |
| 36 | |
| 37 | import com.google.android.collect.Lists; |
| 38 | |
| 39 | import java.util.ArrayList; |
| 40 | import java.util.List; |
| 41 | |
| 42 | public class DocumentsActivity extends Activity { |
| 43 | private static final String TAG = "Documents"; |
| 44 | |
| 45 | @Override |
| 46 | public void onCreate(Bundle icicle) { |
| 47 | super.onCreate(icicle); |
| 48 | |
| 49 | SourceFragment.show(getFragmentManager()); |
| 50 | setResult(Activity.RESULT_CANCELED); |
| 51 | } |
| 52 | |
| 53 | public void onDocumentPicked(Uri uri) { |
| 54 | Log.d(TAG, "onDocumentPicked() " + uri); |
| 55 | |
| 56 | final Intent intent = new Intent(); |
| 57 | intent.setData(uri); |
| 58 | |
| 59 | intent.addFlags( |
| 60 | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_PERSIST_GRANT_URI_PERMISSION); |
| 61 | if (Intent.ACTION_CREATE_DOCUMENT.equals(getIntent().getAction())) { |
| 62 | intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 63 | } |
| 64 | |
| 65 | setResult(Activity.RESULT_OK, intent); |
| 66 | finish(); |
| 67 | } |
| 68 | |
| 69 | public static class SourceFragment extends ListFragment { |
| 70 | private ArrayList<ProviderInfo> mProviders = Lists.newArrayList(); |
| 71 | private ArrayAdapter<ProviderInfo> mAdapter; |
| 72 | |
| 73 | public static void show(FragmentManager fm) { |
| 74 | final SourceFragment fragment = new SourceFragment(); |
| 75 | |
| 76 | final FragmentTransaction ft = fm.beginTransaction(); |
| 77 | ft.replace(android.R.id.content, fragment); |
| 78 | ft.setBreadCrumbTitle("TOP"); |
| 79 | ft.commitAllowingStateLoss(); |
| 80 | } |
| 81 | |
| 82 | @Override |
| 83 | public View onCreateView( |
| 84 | LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
| 85 | final Context context = inflater.getContext(); |
| 86 | |
| 87 | // Gather known storage providers |
| 88 | mProviders.clear(); |
| 89 | final List<ProviderInfo> providers = context.getPackageManager() |
| 90 | .queryContentProviders(null, -1, PackageManager.GET_META_DATA); |
| 91 | for (ProviderInfo info : providers) { |
| 92 | if (info.metaData != null |
| 93 | && info.metaData.containsKey( |
| 94 | DocumentsContract.META_DATA_DOCUMENT_PROVIDER)) { |
| 95 | mProviders.add(info); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | mAdapter = new ArrayAdapter<ProviderInfo>( |
| 100 | context, android.R.layout.simple_list_item_1, mProviders); |
| 101 | setListAdapter(mAdapter); |
| 102 | |
| 103 | return super.onCreateView(inflater, container, savedInstanceState); |
| 104 | } |
| 105 | |
| 106 | @Override |
| 107 | public void onListItemClick(ListView l, View v, int position, long id) { |
| 108 | final ProviderInfo info = mAdapter.getItem(position); |
| 109 | final Uri uri = DocumentsContract.buildContentsUri(DocumentsContract.buildDocumentUri( |
| 110 | info.authority, DocumentsContract.ROOT_GUID)); |
| 111 | final String displayName = info.name; |
| 112 | DirectoryFragment.show(getFragmentManager(), uri, displayName); |
| 113 | } |
| 114 | } |
| 115 | } |