1 /* ownCloud Android client application 
   2  *   Copyright (C) 2012  Bartek Przybylski 
   4  *   This program is free software: you can redistribute it and/or modify 
   5  *   it under the terms of the GNU General Public License as published by 
   6  *   the Free Software Foundation, either version 3 of the License, or 
   7  *   (at your option) any later version. 
   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 eu
.alefzero
.owncloud
.files
; 
  23 import eu
.alefzero
.owncloud
.AccountUtils
; 
  24 import eu
.alefzero
.owncloud
.R
; 
  25 import eu
.alefzero
.owncloud
.authenticator
.AccountAuthenticator
; 
  26 import eu
.alefzero
.owncloud
.db
.DbHandler
; 
  27 import eu
.alefzero
.owncloud
.files
.services
.InstantUploadService
; 
  28 import android
.accounts
.Account
; 
  29 import android
.content
.BroadcastReceiver
; 
  30 import android
.content
.Context
; 
  31 import android
.content
.Intent
; 
  32 import android
.database
.Cursor
; 
  33 import android
.net
.ConnectivityManager
; 
  34 import android
.preference
.Preference
; 
  35 import android
.preference
.PreferenceManager
; 
  36 import android
.provider
.MediaStore
.Images
.Media
; 
  37 import android
.util
.Log
; 
  38 import android
.webkit
.MimeTypeMap
; 
  40 public class PhotoTakenBroadcastReceiver 
extends BroadcastReceiver 
{ 
  42     private static String TAG 
= "PhotoTakenBroadcastReceiver"; 
  43     private static final String
[] CONTENT_PROJECTION 
= { Media
.DATA
, Media
.DISPLAY_NAME
, Media
.MIME_TYPE
, Media
.SIZE 
}; 
  45     private static String NEW_PHOTO_ACTION 
= "com.android.camera.NEW_PICTURE"; 
  48     public void onReceive(Context context
, Intent intent
) { 
  49         if (!PreferenceManager
.getDefaultSharedPreferences(context
).getBoolean("instant_uploading", false
)) { 
  50             Log
.d(TAG
, "Instant upload disabled, abording uploading"); 
  53         if (intent
.getAction().equals(android
.net
.ConnectivityManager
.CONNECTIVITY_ACTION
)) { 
  54             handleConnectivityAction(context
, intent
); 
  55         } else if (intent
.getAction().equals(NEW_PHOTO_ACTION
)) { 
  56             handleNewPhontoAction(context
, intent
); 
  58             Log
.e(TAG
, "Incorrect intent sent: " + intent
.getAction()); 
  62     private void handleNewPhontoAction(Context context
, Intent intent
) { 
  63         Account account 
= AccountUtils
.getCurrentOwnCloudAccount(context
); 
  64         if (account 
== null
) { 
  65             Log
.w(TAG
, "No owncloud account found for instant upload, abording"); 
  69         Cursor c 
= context
.getContentResolver().query(intent
.getData(), CONTENT_PROJECTION
, null
, null
, null
); 
  71         if (!c
.moveToFirst()) { 
  72             Log
.e(TAG
, "Couldn't resolve given uri!"); 
  76         String file_path 
= c
.getString(c
.getColumnIndex(Media
.DATA
)); 
  77         String file_name 
= c
.getString(c
.getColumnIndex(Media
.DISPLAY_NAME
)); 
  78         String mime_type 
= c
.getString(c
.getColumnIndex(Media
.MIME_TYPE
)); 
  79         long file_size 
= c
.getLong(c
.getColumnIndex(Media
.SIZE
)); 
  83         if (!isOnline(context
)) { 
  84             DbHandler db 
= new DbHandler(context
); 
  85             db
.putFileForLater(file_path
, account
.name
); 
  90         Intent upload_intent 
= new Intent(context
, InstantUploadService
.class); 
  91         upload_intent
.putExtra(InstantUploadService
.KEY_ACCOUNT
, account
); 
  92         upload_intent
.putExtra(InstantUploadService
.KEY_FILE_PATH
, file_path
); 
  93         upload_intent
.putExtra(InstantUploadService
.KEY_DISPLAY_NAME
, file_name
); 
  94         upload_intent
.putExtra(InstantUploadService
.KEY_FILE_SIZE
, file_size
); 
  95         upload_intent
.putExtra(InstantUploadService
.KEY_MIME_TYPE
, mime_type
); 
  97         context
.startService(upload_intent
); 
 100     private void handleConnectivityAction(Context context
, Intent intent
) { 
 101         if (!intent
.hasExtra(ConnectivityManager
.EXTRA_NO_CONNECTIVITY
) || 
 103             DbHandler db 
= new DbHandler(context
); 
 104             Cursor c 
= db
.getAwaitingFiles(); 
 105             if (c
.moveToFirst()) { 
 107                     String account_name 
= c
.getString(c
.getColumnIndex("account")); 
 108                     String file_path 
= c
.getString(c
.getColumnIndex("path")); 
 109                     File f 
= new File(file_path
); 
 111                         Intent upload_intent 
= new Intent(context
, InstantUploadService
.class); 
 112                         Account account 
= new Account(account_name
, AccountAuthenticator
.ACCOUNT_TYPE
); 
 114                         String mimeType 
= null
; 
 116                             mimeType 
= MimeTypeMap
.getSingleton() 
 117                                     .getMimeTypeFromExtension( 
 118                                             f
.getName().substring(f
.getName().lastIndexOf('.') + 1)); 
 120                         } catch (IndexOutOfBoundsException e
) { 
 121                             Log
.e(TAG
, "Trying to find out MIME type of a file without extension: " + f
.getName()); 
 123                         if (mimeType 
== null
) 
 124                             mimeType 
= "application/octet-stream"; 
 126                         upload_intent
.putExtra(InstantUploadService
.KEY_ACCOUNT
, account
); 
 127                         upload_intent
.putExtra(InstantUploadService
.KEY_FILE_PATH
, file_path
); 
 128                         upload_intent
.putExtra(InstantUploadService
.KEY_DISPLAY_NAME
, f
.getName()); 
 129                         upload_intent
.putExtra(InstantUploadService
.KEY_FILE_SIZE
, f
.length()); 
 130                         upload_intent
.putExtra(InstantUploadService
.KEY_MIME_TYPE
, mimeType
); 
 132                         context
.startService(upload_intent
); 
 134                         Log
.w(TAG
, "Instant upload file " + f
.getName() + " dont exist anymore"); 
 136                 } while(c
.moveToNext()); 
 145     private boolean isOnline(Context context
) { 
 146         ConnectivityManager cm 
= (ConnectivityManager
) context
.getSystemService(Context
.CONNECTIVITY_SERVICE
); 
 147         return cm
.getActiveNetworkInfo() != null 
&& cm
.getActiveNetworkInfo().isConnected();