| 1 | /* |
| 2 | * Copyright (C) 2008 OpenIntents.org |
| 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 org.openintents.filemanager.demo; |
| 18 | |
| 19 | import java.io.File; |
| 20 | |
| 21 | import org.openintents.intents.FileManagerIntents; |
| 22 | |
| 23 | import android.app.Activity; |
| 24 | import android.content.ActivityNotFoundException; |
| 25 | import android.content.Intent; |
| 26 | import android.database.Cursor; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Bundle; |
| 29 | import android.provider.MediaStore; |
| 30 | import android.provider.MediaStore.Images; |
| 31 | import android.view.View; |
| 32 | import android.widget.EditText; |
| 33 | import android.widget.TextView; |
| 34 | import android.widget.Toast; |
| 35 | |
| 36 | public class Demo extends Activity { |
| 37 | |
| 38 | protected static final int REQUEST_CODE_PICK_FILE_OR_DIRECTORY = 1; |
| 39 | protected static final int REQUEST_CODE_GET_CONTENT = 2; |
| 40 | |
| 41 | protected EditText mEditText; |
| 42 | protected TextView mTextView; |
| 43 | |
| 44 | /** Called when the activity is first created. */ |
| 45 | @Override |
| 46 | public void onCreate(Bundle savedInstanceState) { |
| 47 | super.onCreate(savedInstanceState); |
| 48 | setContentView(R.layout.main); |
| 49 | |
| 50 | mEditText = (EditText) findViewById(R.id.file_path); |
| 51 | mTextView = (TextView) findViewById(R.id.info); |
| 52 | } |
| 53 | |
| 54 | public void onClickOpenFile(View view) { |
| 55 | openFile(); |
| 56 | } |
| 57 | |
| 58 | public void onClickSaveFile(View view) { |
| 59 | saveFile(); |
| 60 | } |
| 61 | |
| 62 | public void onClickPickDirectory(View view) { |
| 63 | pickDirectory(); |
| 64 | } |
| 65 | |
| 66 | public void onClickGetContent(View view) { |
| 67 | getContent(); |
| 68 | } |
| 69 | |
| 70 | public void onClickOpenUri(View view){ |
| 71 | openFileUri(); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Opens the file manager to select a file to open. |
| 76 | */ |
| 77 | public void openFile() { |
| 78 | String fileName = mEditText.getText().toString(); |
| 79 | |
| 80 | Intent intent = new Intent(FileManagerIntents.ACTION_PICK_FILE); |
| 81 | |
| 82 | // Construct URI from file name. |
| 83 | File file = new File(fileName); |
| 84 | intent.setData(Uri.fromFile(file)); |
| 85 | |
| 86 | // Set fancy title and button (optional) |
| 87 | intent.putExtra(FileManagerIntents.EXTRA_TITLE, getString(R.string.open_title)); |
| 88 | intent.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT, getString(R.string.open_button)); |
| 89 | |
| 90 | try { |
| 91 | startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY); |
| 92 | } catch (ActivityNotFoundException e) { |
| 93 | // No compatible file manager was found. |
| 94 | Toast.makeText(this, R.string.no_filemanager_installed, |
| 95 | Toast.LENGTH_SHORT).show(); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Opens the file manager to select a location for saving a file. |
| 101 | */ |
| 102 | private void saveFile() { |
| 103 | String fileName = mEditText.getText().toString(); |
| 104 | |
| 105 | Intent intent = new Intent(FileManagerIntents.ACTION_PICK_FILE); |
| 106 | |
| 107 | // Construct URI from file name. |
| 108 | File file = new File(fileName); |
| 109 | intent.setData(Uri.fromFile(file)); |
| 110 | |
| 111 | // Set fancy title and button (optional) |
| 112 | intent.putExtra(FileManagerIntents.EXTRA_TITLE, getString(R.string.save_title)); |
| 113 | intent.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT, getString(R.string.save_button)); |
| 114 | |
| 115 | try { |
| 116 | startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY); |
| 117 | } catch (ActivityNotFoundException e) { |
| 118 | // No compatible file manager was found. |
| 119 | Toast.makeText(this, R.string.no_filemanager_installed, |
| 120 | Toast.LENGTH_SHORT).show(); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Opens the file manager to pick a directory. |
| 126 | */ |
| 127 | private void pickDirectory() { |
| 128 | String fileName = mEditText.getText().toString(); |
| 129 | |
| 130 | // Note the different intent: PICK_DIRECTORY |
| 131 | Intent intent = new Intent(FileManagerIntents.ACTION_PICK_DIRECTORY); |
| 132 | |
| 133 | // Construct URI from file name. |
| 134 | File file = new File(fileName); |
| 135 | intent.setData(Uri.fromFile(file)); |
| 136 | |
| 137 | // Set fancy title and button (optional) |
| 138 | intent.putExtra(FileManagerIntents.EXTRA_TITLE, getString(R.string.pick_directory_title)); |
| 139 | intent.putExtra(FileManagerIntents.EXTRA_BUTTON_TEXT, getString(R.string.pick_directory_button)); |
| 140 | |
| 141 | try { |
| 142 | startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY); |
| 143 | } catch (ActivityNotFoundException e) { |
| 144 | // No compatible file manager was found. |
| 145 | Toast.makeText(this, R.string.no_filemanager_installed, |
| 146 | Toast.LENGTH_SHORT).show(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Use GET_CONTENT to open a file. |
| 152 | */ |
| 153 | public void getContent() { |
| 154 | |
| 155 | Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 156 | intent.setType("*/*"); |
| 157 | intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 158 | |
| 159 | try { |
| 160 | startActivityForResult(intent, REQUEST_CODE_GET_CONTENT); |
| 161 | } catch (ActivityNotFoundException e) { |
| 162 | // No compatible file manager was found. |
| 163 | Toast.makeText(this, R.string.no_filemanager_installed, |
| 164 | Toast.LENGTH_SHORT).show(); |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Use URI to get a file |
| 170 | */ |
| 171 | public void openFileUri(){ |
| 172 | String filePath = mEditText.getText().toString(); |
| 173 | Uri uri = Uri.parse("file://" + filePath); |
| 174 | |
| 175 | Intent intent = new Intent(Intent.ACTION_VIEW); |
| 176 | intent.setData(uri); |
| 177 | |
| 178 | // optionally: |
| 179 | //intent.setPackage("org.openintents.filemanager"); |
| 180 | |
| 181 | try { |
| 182 | startActivity(intent); |
| 183 | } catch (ActivityNotFoundException e) { |
| 184 | // No compatible file manager was found. |
| 185 | Toast.makeText(this, R.string.no_filemanager_installed, |
| 186 | Toast.LENGTH_SHORT).show(); |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * This is called after the file manager finished. |
| 192 | */ |
| 193 | @Override |
| 194 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { |
| 195 | super.onActivityResult(requestCode, resultCode, data); |
| 196 | |
| 197 | mTextView.setText(""); |
| 198 | |
| 199 | switch (requestCode) { |
| 200 | case REQUEST_CODE_PICK_FILE_OR_DIRECTORY: |
| 201 | if (resultCode == RESULT_OK && data != null) { |
| 202 | // obtain the filename |
| 203 | Uri fileUri = data.getData(); |
| 204 | if (fileUri != null) { |
| 205 | String filePath = fileUri.getPath(); |
| 206 | if (filePath != null) { |
| 207 | mEditText.setText(filePath); |
| 208 | } |
| 209 | } |
| 210 | } |
| 211 | break; |
| 212 | case REQUEST_CODE_GET_CONTENT: |
| 213 | if (resultCode == RESULT_OK && data != null) { |
| 214 | String filePath = null; |
| 215 | long fileSize = 0; |
| 216 | String displayName = null; |
| 217 | Uri uri = data.getData(); |
| 218 | Cursor c = getContentResolver().query(uri, new String[] {MediaStore.MediaColumns.DATA, |
| 219 | MediaStore.MediaColumns.MIME_TYPE, |
| 220 | MediaStore.MediaColumns.DISPLAY_NAME, |
| 221 | MediaStore.MediaColumns.SIZE |
| 222 | }, null, null, null); |
| 223 | if (c != null && c.moveToFirst()) { |
| 224 | int id = c.getColumnIndex(Images.Media.DATA); |
| 225 | if (id != -1) { |
| 226 | filePath = c.getString(id); |
| 227 | } |
| 228 | displayName = c.getString(2); |
| 229 | fileSize = c.getLong(3); |
| 230 | } |
| 231 | if (filePath != null) { |
| 232 | mEditText.setText(filePath); |
| 233 | String strFileSize = getString(R.string.get_content_info, |
| 234 | displayName, "" + fileSize); |
| 235 | mTextView.setText(strFileSize); |
| 236 | } |
| 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |