removed the discussion about binary file upload, as it's now implemented that way
[sendtowebdav:sendtowebdav.git] / src / de / binaervarianz / sendtowebdav / SendToServer.java
1 package de.binaervarianz.sendtowebdav;
2
3 import java.io.IOException;
4 import java.net.URI;
5 import java.net.URISyntaxException;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8
9 import org.apache.http.HttpException;
10 import org.apache.http.client.ClientProtocolException;
11
12 import android.app.Activity;
13 import android.content.Context;
14 import android.content.Intent;
15 import android.content.SharedPreferences;
16 import android.database.Cursor;
17 import android.net.Uri;
18 import android.os.Bundle;
19 import android.provider.MediaStore;
20 import android.util.Log;
21 import android.widget.Toast;
22
23 public class SendToServer extends Activity {
24         private final String TAG = this.getClass().getName();
25         
26         private WebDAVhandler httpHandler;
27
28         @Override
29         public void onCreate(Bundle savedInstanceState) {
30                 super.onCreate(savedInstanceState);
31
32                 // read preferences
33                 SharedPreferences settings = getSharedPreferences(ConfigWebDAV.PREFS_PRIVATE, Context.MODE_PRIVATE);
34                 String serverURI = settings.getString(ConfigWebDAV.KEY_SERVER_URI, "https://");
35                 boolean trustAllSSLCerts = settings.getBoolean(ConfigWebDAV.KEY_TRUSTALLSSL, false);
36                 String user = settings.getString(ConfigWebDAV.KEY_USERNAME, "");
37                 String pass = settings.getString(ConfigWebDAV.KEY_PASSWORD, "");
38
39                 try {
40                         new URI(serverURI);
41                 } catch (URISyntaxException e) {
42                         Log.e(TAG, "URISyntaxException: "+e);
43                         // TODO: user error message (maybe also do this check when testing/saving) -- is inherently done!?
44                         // TODO: alert dialog notifying users of broken url and redirecting them to the config app
45                         // "Please provide a valid Server URI in the Config App first"
46                         return;
47                 }
48                 
49                 httpHandler = new WebDAVhandler(serverURI, user, pass);
50                 httpHandler.setTrustAllSSLCerts(trustAllSSLCerts);
51
52                 Intent intent = getIntent();
53                 Bundle extras = intent.getExtras();
54                 
55                 if (intent.getAction().equals(Intent.ACTION_SEND) && extras != null) {
56                 //TODO figure out what's to be send
57                 
58                         //this is for debugging and getting information on new media types
59                         String type = intent.resolveType(this);
60                         //URLS give text/plain, images image/jpeg ....
61                         Log.d(TAG, type);
62                         for (String s : extras.keySet()) {
63                                 Log.d(TAG, s);
64                                 //Urls give EXTRA_TEXT, images EXTRA_STREAM
65                         }
66                         if (intent.getDataString() != null) {
67                                 Log.d(TAG, intent.getDataString());     
68                                 //no data so far attached to the intents
69                         }                       
70                         /// end of debug, resume normal operation                       
71                         
72                         String url = "";
73                         SimpleDateFormat dateformater = new SimpleDateFormat("yyyyMMddHHmmss"); 
74                         try {                           
75                                 if (extras.containsKey(Intent.EXTRA_TEXT)) {    // simple text like URLs
76                                         url += extras.getString(Intent.EXTRA_TEXT);
77                                         httpHandler.putFile("URL-"+dateformater.format(new Date())+".txt", "", url);
78                                         
79                                 } else if (extras.containsKey(Intent.EXTRA_STREAM)) {   // binary files
80                                         Uri contentUri = (Uri) extras.get(Intent.EXTRA_STREAM);
81                                         
82                                         //debug
83                                         Log.d(TAG, contentUri.toString());
84                                         
85                                         String filePath = "";
86                                         // there are real file system paths and logical content URIs
87                                         // I've so far only tested the first kind [chaos]
88                                         if (contentUri.toString().startsWith("content:")) {
89                                                 filePath = this.getRealPathFromURI(contentUri);
90                                         } else {
91                                                 filePath = contentUri.getEncodedPath();
92                                         }
93                                         // create a basename out of the type identifier
94                                         String basename = type.toUpperCase().charAt(0) + type.substring(1, type.indexOf('/'));
95                                         String name = basename + "-" + dateformater.format(new Date());
96                                         
97                                         //debug
98                                         Log.d(TAG, name);
99
100                                         httpHandler.putBinFile( name, "", filePath, type);
101                                 }
102                         } catch (ClientProtocolException e) {
103                                 Toast.makeText(this, this.getString(R.string.app_name) + ": ClientProtocolException: "+e, Toast.LENGTH_LONG).show();
104                                 Log.e(TAG, "ClientProtocolException: "+e);
105                                 return;
106                         } catch (IOException e) {
107                                 Toast.makeText(this, this.getString(R.string.app_name) + ": IOException: "+e, Toast.LENGTH_LONG).show();
108                                 Log.e(TAG, "IOException: "+e);
109                                 return;
110                         } catch (IllegalArgumentException e) {
111                                 Toast.makeText(this, this.getString(R.string.app_name) + ": IllegalArgumentException: "+e, Toast.LENGTH_LONG).show();
112                                 Log.e(TAG, "IllegalArgumentException: "+e);
113                                 return;
114                         } catch (HttpException e) {
115                                 Toast.makeText(this, this.getString(R.string.app_name) + ": HttpException: "+e, Toast.LENGTH_LONG).show();
116                                 Log.e(TAG, "HttpException: "+e);
117                                 return;
118                         }
119                         
120                         Toast.makeText(this, R.string.data_send, Toast.LENGTH_SHORT).show();
121                 }
122                 
123         }
124         /**
125          * convert a <content:> URI (like from the gallery) to an absolute path
126          * 
127          * This is stolen from http://stackoverflow.com/questions/3401579/android-get-filename-and-path-from-uri-from-mediastore
128          * @param contentUri
129          * @return String with absolute path to file
130          */
131         private String getRealPathFromURI(Uri contentUri) {
132         String[] proj = { MediaStore.Images.Media.DATA };
133         Cursor cursor = managedQuery(contentUri, proj, null, null, null);
134         int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
135         cursor.moveToFirst();
136         return cursor.getString(column_index);
137     }
138 }