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