add OI File Manager and AndroidSupportV2 used by it
[android_pandora.git] / apps / oi-filemanager / FileManager / src / org / openintents / filemanager / SaveAsActivity.java
diff --git a/apps/oi-filemanager/FileManager/src/org/openintents/filemanager/SaveAsActivity.java b/apps/oi-filemanager/FileManager/src/org/openintents/filemanager/SaveAsActivity.java
new file mode 100644 (file)
index 0000000..aa1b21e
--- /dev/null
@@ -0,0 +1,152 @@
+package org.openintents.filemanager;\r
+\r
+import java.io.BufferedInputStream;\r
+import java.io.BufferedOutputStream;\r
+import java.io.File;\r
+import java.io.FileInputStream;\r
+import java.io.FileNotFoundException;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+\r
+import org.openintents.intents.FileManagerIntents;\r
+\r
+import android.app.Activity;\r
+import android.content.ActivityNotFoundException;\r
+import android.content.Intent;\r
+import android.database.Cursor;\r
+import android.net.Uri;\r
+import android.os.Bundle;\r
+import android.os.Environment;\r
+import android.widget.RelativeLayout;\r
+import android.widget.Toast;\r
+\r
+public class SaveAsActivity extends Activity {\r
+       protected static final int REQUEST_CODE_PICK_FILE_OR_DIRECTORY = 1;\r
+       private Uri source;\r
+       //Whether the scheme is file: (otherwise it's content:)\r
+       private boolean fileScheme = false;\r
+       \r
+       \r
+       @Override\r
+    public void onCreate(Bundle savedInstanceState) {\r
+        super.onCreate(savedInstanceState);\r
+        //This activity is never shown to the user.\r
+        setContentView(new RelativeLayout(this));\r
+        Intent receivedIntent = getIntent();\r
+        if(receivedIntent != null){\r
+               Uri uri = receivedIntent.getData();\r
+               source = uri;\r
+               if(uri.getScheme().equals("file"))\r
+                       processFile(uri);\r
+               else if(uri.getScheme().equals("content"))\r
+                       processContent(uri);\r
+        }\r
+        else{\r
+                       Toast.makeText(this, R.string.saveas_no_file_picked, Toast.LENGTH_SHORT).show();\r
+        }\r
+    }\r
+       \r
+       private void startPickActivity(Intent intent){\r
+               try {\r
+                       startActivityForResult(intent, REQUEST_CODE_PICK_FILE_OR_DIRECTORY);\r
+               } catch (ActivityNotFoundException e) {\r
+                       //Should never happen, but Java requires this catch\r
+                       Toast.makeText(this, R.string.saveas_error, Toast.LENGTH_SHORT).show();\r
+               }\r
+       }\r
+       \r
+       private Intent createPickIntent(){\r
+               return new Intent(FileManagerIntents.ACTION_PICK_FILE);\r
+       }\r
+       \r
+       private void processFile(Uri uri){\r
+               fileScheme = true;\r
+               Intent intent = createPickIntent();\r
+               intent.setData(uri);\r
+               startPickActivity(intent);\r
+       }\r
+       \r
+       private void processContent(Uri uri){\r
+               fileScheme = false;\r
+               String name = getPath(uri);\r
+               Intent intent = createPickIntent();\r
+               intent.setData(Uri.parse(name));\r
+               startPickActivity(intent);\r
+       }\r
+       \r
+       /*\r
+        * Get the default path and filename for the saved file from content: scheme.\r
+        * As the directory is always used the SD storage.\r
+        * For GMail, the filename is the _display_name in its ContentProvider. Otherwise the file has\r
+        * no name.\r
+        * !IMPORTANT! When you add another "special" intent-filter like the one for GMail, consider,\r
+        * if you could add also the code for finding out the filename.\r
+        */\r
+       private String getPath(Uri uri){\r
+               Uri sd = Uri.fromFile(Environment.getExternalStorageDirectory());\r
+               if(uri.getHost().equals("gmail-ls")){\r
+                       Cursor cur = managedQuery(uri, new String[]{"_display_name"}, null, null, null);\r
+                       int nameColumn = cur.getColumnIndex("_display_name"); \r
+                       if(cur.moveToFirst()){\r
+                               return sd.buildUpon().appendPath(cur.getString(nameColumn)).toString();\r
+                       }\r
+               }\r
+               return sd.getPath();\r
+       }\r
+    \r
+    @Override\r
+       protected void onActivityResult(int requestCode, int resultCode, Intent data) {\r
+               super.onActivityResult(requestCode, resultCode, data);\r
+               \r
+               switch (requestCode) {\r
+               case REQUEST_CODE_PICK_FILE_OR_DIRECTORY:\r
+                       if (resultCode == RESULT_OK && data != null) {\r
+                               Uri destinationUri = data.getData();\r
+                               if (destinationUri != null && source != null) {\r
+                                       String destinationPath = destinationUri.getPath();\r
+                                       saveFile(new File(destinationPath));\r
+                               }\r
+                       }\r
+                       break;\r
+               }\r
+               finish(); //End the activity\r
+       }\r
+    \r
+    private void saveFile(File destination){\r
+               InputStream in = null;\r
+               OutputStream out = null;\r
+               try {\r
+                       if(fileScheme)\r
+                               in = new BufferedInputStream(new FileInputStream(source.getPath()));\r
+                       else\r
+                               in = new BufferedInputStream(getContentResolver().openInputStream(source));\r
+                       \r
+                       out = new BufferedOutputStream(new FileOutputStream(destination));\r
+               byte[] buffer = new byte[1024];\r
+               \r
+               while(in.read(buffer) != -1)\r
+                       out.write(buffer);\r
+                       Toast.makeText(this, R.string.saveas_file_saved, Toast.LENGTH_SHORT).show();\r
+               } catch(FileNotFoundException e){\r
+                       //Should never get here\r
+                       Toast.makeText(this, R.string.saveas_error, Toast.LENGTH_SHORT).show();\r
+               } catch(IOException e){\r
+                       Toast.makeText(this, R.string.saveas_error, Toast.LENGTH_SHORT).show();\r
+               }\r
+               finally{\r
+                       if (in != null) {\r
+                               try {\r
+                                       in.close();\r
+                               } catch (IOException e) {}\r
+                       }\r
+                       if (out != null) {\r
+                               try {\r
+                                       out.close();\r
+                               } catch (IOException e) {}\r
+                       }\r
+                       \r
+               }\r
+    }\r
+}\r