1 /* ownCloud Android client application 
   2  *   Copyright (C) 2012  Bartek Przybylski 
   3  *   Copyright (C) 2012-2013 ownCloud Inc. 
   5  *   This program is free software: you can redistribute it and/or modify 
   6  *   it under the terms of the GNU General Public License version 2, 
   7  *   as published by the Free Software Foundation. 
   9  *   This program is distributed in the hope that it will be useful, 
  10  *   but WITHOUT ANY WARRANTY; without even the implied warranty of 
  11  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
  12  *   GNU General Public License for more details. 
  14  *   You should have received a copy of the GNU General Public License 
  15  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. 
  19 package com
.owncloud
.android
.files
; 
  23 import android
.accounts
.Account
; 
  24 import android
.content
.BroadcastReceiver
; 
  25 import android
.content
.Context
; 
  26 import android
.content
.Intent
; 
  27 import android
.content
.IntentFilter
; 
  28 import android
.database
.Cursor
; 
  29 import android
.net
.ConnectivityManager
; 
  30 import android
.net
.NetworkInfo
.State
; 
  31 import android
.preference
.PreferenceManager
; 
  32 import android
.provider
.MediaStore
.Images
.Media
; 
  33 import android
.webkit
.MimeTypeMap
; 
  35 import com
.owncloud
.android
.AccountUtils
; 
  36 import com
.owncloud
.android
.Log_OC
; 
  37 import com
.owncloud
.android
.authenticator
.AccountAuthenticator
; 
  38 import com
.owncloud
.android
.db
.DbHandler
; 
  39 import com
.owncloud
.android
.files
.services
.FileUploader
; 
  40 import com
.owncloud
.android
.utils
.FileStorageUtils
; 
  42 public class InstantUploadBroadcastReceiver 
extends BroadcastReceiver 
{ 
  44     private static String TAG 
= "PhotoTakenBroadcastReceiver"; 
  45     private static final String
[] CONTENT_PROJECTION 
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE 
}; 
  46     private static String NEW_PHOTO_ACTION 
= "com.android.camera.NEW_PICTURE"; 
  49     public void onReceive(Context context
, Intent intent
) { 
  50         Log_OC
.d(TAG
, "Received: " + intent
.getAction()); 
  51         if (intent
.getAction().equals(android
.net
.ConnectivityManager
.CONNECTIVITY_ACTION
)) { 
  52             handleConnectivityAction(context
, intent
); 
  53         } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) { 
  54             handleNewPhotoAction(context
, intent
); 
  55         } else if (intent
.getAction().equals(FileUploader
.UPLOAD_FINISH_MESSAGE
)) { 
  56             handleUploadFinished(context
, intent
); 
  58             Log_OC
.e(TAG
, "Incorrect intent sent: " + intent
.getAction()); 
  62     private void handleUploadFinished(Context context
, Intent intent
) { 
  63         // remove successfull uploading, ignore rest for reupload on reconnect 
  64         if (intent
.getBooleanExtra(FileUploader
.EXTRA_UPLOAD_RESULT
, false
)) { 
  65             DbHandler db 
= new DbHandler(context
); 
  66             String localPath 
= intent
.getStringExtra(FileUploader
.EXTRA_OLD_FILE_PATH
); 
  67             if (!db
.removeIUPendingFile(localPath
)) { 
  68                 Log_OC
.w(TAG
, "Tried to remove non existing instant upload file " + localPath
); 
  74     private void handleNewPhotoAction(Context context
, Intent intent
) { 
  75         if (!instantUploadEnabled(context
)) { 
  76             Log_OC
.d(TAG
, "Instant upload disabled, abording uploading"); 
  80         Account account 
= AccountUtils
.getCurrentOwnCloudAccount(context
); 
  81         if (account 
== null
) { 
  82             Log_OC
.w(TAG
, "No owncloud account found for instant upload, aborting"); 
  86         Cursor c 
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
); 
  88         if (!c
.moveToFirst()) { 
  89             Log_OC
.e(TAG
, "Couldn't resolve given uri: " + intent
.getDataString()); 
  93         String file_path 
= c
.getString(c
.getColumnIndex(Media
.DATA
)); 
  94         String file_name 
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
)); 
  95         String mime_type 
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
)); 
  98         Log_OC
.e(TAG
, file_path 
+ ""); 
 100         // same always temporally the picture to upload 
 101         DbHandler db 
= new DbHandler(context
); 
 102         db
.putFileForLater(file_path
, account
.name
, null
); 
 105         if (!isOnline(context
) || (instantUploadViaWiFiOnly(context
) && !isConnectedViaWiFi(context
))) { 
 109         // register for upload finishe message 
 110         // there is a litte problem with android API, we can register for 
 112         // intent in registerReceiver but we cannot unregister from precise 
 114         // we can unregister from entire listenings but thats suck a bit. 
 115         // On the other hand this might be only for dynamicly registered 
 116         // broadcast receivers, needs investigation. 
 117         IntentFilter filter 
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
); 
 118         context
.getApplicationContext().registerReceiver(this, filter
); 
 120         Intent i 
= new Intent(context
, FileUploader
.class); 
 121         i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
); 
 122         i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
); 
 123         i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantUploadFilePath(context
, file_name
)); 
 124         i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
); 
 125         i
.putExtra(FileUploader
.KEY_MIME_TYPE
, mime_type
); 
 126         i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
); 
 127         context
.startService(i
); 
 131     private void handleConnectivityAction(Context context
, Intent intent
) { 
 132         if (!instantUploadEnabled(context
)) { 
 133             Log_OC
.d(TAG
, "Instant upload disabled, abording uploading"); 
 137         if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
) 
 139                 && (!instantUploadViaWiFiOnly(context
) || (instantUploadViaWiFiOnly(context
) == isConnectedViaWiFi(context
) == true
))) { 
 140             DbHandler db 
= new DbHandler(context
); 
 141             Cursor c 
= db
.getAwaitingFiles(); 
 142             if (c
.moveToFirst()) { 
 143                 IntentFilter filter 
= new IntentFilter(FileUploader
.UPLOAD_FINISH_MESSAGE
); 
 144                 context
.getApplicationContext().registerReceiver(this, filter
); 
 146                     String account_name 
= c
.getString(c
.getColumnIndex("account")); 
 147                     String file_path 
= c
.getString(c
.getColumnIndex("path")); 
 148                     File f 
= new File(file_path
); 
 150                         Account account 
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
); 
 152                         String mimeType 
= null
; 
 154                             mimeType 
= MimeTypeMap
.getSingleton().getMimeTypeFromExtension( 
 155                                     f
.getName().substring(f
.getName().lastIndexOf('.') + 1)); 
 157                         } catch (Throwable e
) { 
 158                             Log_OC
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName()); 
 160                         if (mimeType 
== null
) 
 161                             mimeType 
= "application/octet-stream"; 
 163                         Intent i 
= new Intent(context
, FileUploader
.class); 
 164                         i
.putExtra(FileUploader
.KEY_ACCOUNT
, account
); 
 165                         i
.putExtra(FileUploader
.KEY_LOCAL_FILE
, file_path
); 
 166                         i
.putExtra(FileUploader
.KEY_REMOTE_FILE
, FileStorageUtils
.getInstantUploadFilePath(context
, f
.getName())); 
 167                         i
.putExtra(FileUploader
.KEY_UPLOAD_TYPE
, FileUploader
.UPLOAD_SINGLE_FILE
); 
 168                         i
.putExtra(FileUploader
.KEY_INSTANT_UPLOAD
, true
); 
 169                         context
.startService(i
); 
 172                         Log_OC
.w(TAG
, "Instant upload file " + f
.getAbsolutePath() + " dont exist anymore"); 
 174                 } while (c
.moveToNext()); 
 182     public static boolean isOnline(Context context
) { 
 183         ConnectivityManager cm 
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
); 
 184         return cm
.getActiveNetworkInfo() != null 
&& cm
.getActiveNetworkInfo().isConnected(); 
 187     public static boolean isConnectedViaWiFi(Context context
) { 
 188         ConnectivityManager cm 
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
); 
 189         return cm 
!= null 
&& cm
.getActiveNetworkInfo() != null
 
 190                 && cm
.getActiveNetworkInfo().getType() == ConnectivityManager
.TYPE_WIFI
 
 191                 && cm
.getActiveNetworkInfo().getState() == State
.CONNECTED
; 
 194     public static boolean instantUploadEnabled(Context context
) { 
 195         return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
); 
 198     public static boolean instantUploadViaWiFiOnly(Context context
) { 
 199         return PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_upload_on_wifi", false
);