Home | History | Annotate | Download | only in documentsui
      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.content.Context;
     21 import android.content.Intent;
     22 import android.net.Uri;
     23 import android.os.Bundle;
     24 import android.provider.DocumentsContract;
     25 import android.util.Log;
     26 import android.view.View;
     27 import android.view.View.OnClickListener;
     28 import android.widget.Button;
     29 import android.widget.CheckBox;
     30 import android.widget.LinearLayout;
     31 import android.widget.ScrollView;
     32 import android.widget.TextView;
     33 
     34 import libcore.io.IoUtils;
     35 import libcore.io.Streams;
     36 
     37 import java.io.InputStream;
     38 import java.io.OutputStream;
     39 
     40 public class TestActivity extends Activity {
     41     private static final String TAG = "TestActivity";
     42 
     43     private static final int CODE_READ = 42;
     44     private static final int CODE_WRITE = 43;
     45 
     46     private TextView mResult;
     47 
     48     @Override
     49     public void onCreate(Bundle icicle) {
     50         super.onCreate(icicle);
     51 
     52         final Context context = this;
     53 
     54         final LinearLayout view = new LinearLayout(context);
     55         view.setOrientation(LinearLayout.VERTICAL);
     56 
     57         mResult = new TextView(context);
     58         view.addView(mResult);
     59 
     60         final CheckBox multiple = new CheckBox(context);
     61         multiple.setText("ALLOW_MULTIPLE");
     62         view.addView(multiple);
     63         final CheckBox localOnly = new CheckBox(context);
     64         localOnly.setText("LOCAL_ONLY");
     65         view.addView(localOnly);
     66 
     67         Button button;
     68         button = new Button(context);
     69         button.setText("OPEN_DOC */*");
     70         button.setOnClickListener(new OnClickListener() {
     71             @Override
     72             public void onClick(View v) {
     73                 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
     74                 intent.addCategory(Intent.CATEGORY_OPENABLE);
     75                 intent.setType("*/*");
     76                 if (multiple.isChecked()) {
     77                     intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
     78                 }
     79                 if (localOnly.isChecked()) {
     80                     intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
     81                 }
     82                 startActivityForResult(intent, CODE_READ);
     83             }
     84         });
     85         view.addView(button);
     86 
     87         button = new Button(context);
     88         button.setText("OPEN_DOC image/*");
     89         button.setOnClickListener(new OnClickListener() {
     90             @Override
     91             public void onClick(View v) {
     92                 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
     93                 intent.addCategory(Intent.CATEGORY_OPENABLE);
     94                 intent.setType("image/*");
     95                 if (multiple.isChecked()) {
     96                     intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
     97                 }
     98                 if (localOnly.isChecked()) {
     99                     intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    100                 }
    101                 startActivityForResult(intent, CODE_READ);
    102             }
    103         });
    104         view.addView(button);
    105 
    106         button = new Button(context);
    107         button.setText("OPEN_DOC audio/ogg");
    108         button.setOnClickListener(new OnClickListener() {
    109             @Override
    110             public void onClick(View v) {
    111                 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    112                 intent.addCategory(Intent.CATEGORY_OPENABLE);
    113                 intent.setType("audio/ogg");
    114                 if (multiple.isChecked()) {
    115                     intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    116                 }
    117                 if (localOnly.isChecked()) {
    118                     intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    119                 }
    120                 startActivityForResult(intent, CODE_READ);
    121             }
    122         });
    123         view.addView(button);
    124 
    125         button = new Button(context);
    126         button.setText("OPEN_DOC text/plain, application/msword");
    127         button.setOnClickListener(new OnClickListener() {
    128             @Override
    129             public void onClick(View v) {
    130                 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    131                 intent.addCategory(Intent.CATEGORY_OPENABLE);
    132                 intent.setType("*/*");
    133                 intent.putExtra(Intent.EXTRA_MIME_TYPES, new String[] {
    134                         "text/plain", "application/msword" });
    135                 if (multiple.isChecked()) {
    136                     intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    137                 }
    138                 if (localOnly.isChecked()) {
    139                     intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    140                 }
    141                 startActivityForResult(intent, CODE_READ);
    142             }
    143         });
    144         view.addView(button);
    145 
    146         button = new Button(context);
    147         button.setText("CREATE_DOC text/plain");
    148         button.setOnClickListener(new OnClickListener() {
    149             @Override
    150             public void onClick(View v) {
    151                 Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    152                 intent.addCategory(Intent.CATEGORY_OPENABLE);
    153                 intent.setType("text/plain");
    154                 intent.putExtra(Intent.EXTRA_TITLE, "foobar.txt");
    155                 if (localOnly.isChecked()) {
    156                     intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    157                 }
    158                 startActivityForResult(intent, CODE_WRITE);
    159             }
    160         });
    161         view.addView(button);
    162 
    163         button = new Button(context);
    164         button.setText("CREATE_DOC image/png");
    165         button.setOnClickListener(new OnClickListener() {
    166             @Override
    167             public void onClick(View v) {
    168                 Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
    169                 intent.addCategory(Intent.CATEGORY_OPENABLE);
    170                 intent.setType("image/png");
    171                 intent.putExtra(Intent.EXTRA_TITLE, "mypicture.png");
    172                 if (localOnly.isChecked()) {
    173                     intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    174                 }
    175                 startActivityForResult(intent, CODE_WRITE);
    176             }
    177         });
    178         view.addView(button);
    179 
    180         button = new Button(context);
    181         button.setText("GET_CONTENT */*");
    182         button.setOnClickListener(new OnClickListener() {
    183             @Override
    184             public void onClick(View v) {
    185                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    186                 intent.addCategory(Intent.CATEGORY_OPENABLE);
    187                 intent.setType("*/*");
    188                 if (multiple.isChecked()) {
    189                     intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
    190                 }
    191                 if (localOnly.isChecked()) {
    192                     intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
    193                 }
    194                 startActivityForResult(Intent.createChooser(intent, "Kittens!"), CODE_READ);
    195             }
    196         });
    197         view.addView(button);
    198 
    199         final ScrollView scroll = new ScrollView(context);
    200         scroll.addView(view);
    201 
    202         setContentView(scroll);
    203     }
    204 
    205     @Override
    206     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    207         mResult.setText(null);
    208         String result = "resultCode=" + resultCode + ", data=" + String.valueOf(data);
    209 
    210         if (requestCode == CODE_READ) {
    211             final Uri uri = data != null ? data.getData() : null;
    212             if (uri != null) {
    213                 if (DocumentsContract.isDocumentUri(this, uri)) {
    214                     result += "; DOC_ID";
    215                 }
    216                 try {
    217                     getContentResolver().takePersistableUriPermission(
    218                             uri, Intent.FLAG_GRANT_READ_URI_PERMISSION);
    219                 } catch (SecurityException e) {
    220                     result += "; FAILED TO TAKE";
    221                     Log.e(TAG, "Failed to take", e);
    222                 }
    223                 InputStream is = null;
    224                 try {
    225                     is = getContentResolver().openInputStream(uri);
    226                     final int length = Streams.readFullyNoClose(is).length;
    227                     result += "; read length=" + length;
    228                 } catch (Exception e) {
    229                     result += "; ERROR";
    230                     Log.e(TAG, "Failed to read " + uri, e);
    231                 } finally {
    232                     IoUtils.closeQuietly(is);
    233                 }
    234             } else {
    235                 result += "no uri?";
    236             }
    237         } else if (requestCode == CODE_WRITE) {
    238             final Uri uri = data != null ? data.getData() : null;
    239             if (uri != null) {
    240                 if (DocumentsContract.isDocumentUri(this, uri)) {
    241                     result += "; DOC_ID";
    242                 }
    243                 try {
    244                     getContentResolver().takePersistableUriPermission(
    245                             uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    246                 } catch (SecurityException e) {
    247                     result += "; FAILED TO TAKE";
    248                     Log.e(TAG, "Failed to take", e);
    249                 }
    250                 OutputStream os = null;
    251                 try {
    252                     os = getContentResolver().openOutputStream(uri);
    253                     os.write("THE COMPLETE WORKS OF SHAKESPEARE".getBytes());
    254                 } catch (Exception e) {
    255                     result += "; ERROR";
    256                     Log.e(TAG, "Failed to write " + uri, e);
    257                 } finally {
    258                     IoUtils.closeQuietly(os);
    259                 }
    260             } else {
    261                 result += "no uri?";
    262             }
    263         }
    264 
    265         Log.d(TAG, result);
    266         mResult.setText(result);
    267     }
    268 }
    269