From 23e3c3ffcc0606a472136cd97b49f7af45530d41 Mon Sep 17 00:00:00 2001 From: Mohit Date: Thu, 9 Jul 2020 12:18:25 +0530 Subject: [PATCH] - Removed sharedUserId - Fix issue when eDrive doesn't recognise User account after update --- app/src/main/AndroidManifest.xml | 54 ++++--- .../drive/receivers/BootCompleteReceiver.java | 38 ++++- .../foundation/e/drive/utils/CommonUtils.java | 139 +++++++++++------- app/src/main/res/values/strings.xml | 1 + 4 files changed, 148 insertions(+), 84 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 8ac62cd4..792349d7 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,16 +7,15 @@ http://www.gnu.org/licenses/gpl.html --> + package="foundation.e.drive"> - - - + + + + android:exported="true" + android:label="Pictures and videos" /> + android:exported="true" + android:label="Application settings" /> - - - - - + + - - + - - + + - + - + diff --git a/app/src/main/java/foundation/e/drive/receivers/BootCompleteReceiver.java b/app/src/main/java/foundation/e/drive/receivers/BootCompleteReceiver.java index 5c7b9199..bdab453b 100644 --- a/app/src/main/java/foundation/e/drive/receivers/BootCompleteReceiver.java +++ b/app/src/main/java/foundation/e/drive/receivers/BootCompleteReceiver.java @@ -1,5 +1,6 @@ package foundation.e.drive.receivers; +import android.accounts.Account; import android.accounts.AccountManager; import android.content.BroadcastReceiver; import android.content.Context; @@ -7,32 +8,55 @@ import android.content.Intent; import android.content.SharedPreferences; import android.util.Log; +import foundation.e.drive.R; import foundation.e.drive.services.ObserverService; import foundation.e.drive.utils.AppConstants; import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.utils.JobUtils; - public class BootCompleteReceiver extends BroadcastReceiver { private final static String TAG = BootCompleteReceiver.class.getSimpleName(); + @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "onReceive"); String intentAction = intent.getAction(); - if(intentAction == null){ + SharedPreferences prefs = context.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); + + if (intentAction == null) { Log.e(TAG, "intent Action is null"); - } else if ( intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { + } else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) { - SharedPreferences prefs = context.getSharedPreferences(AppConstants.SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); - if (prefs.getString(AccountManager.KEY_ACCOUNT_NAME, null) != null){ + if (prefs.getString(AccountManager.KEY_ACCOUNT_NAME, null) != null) { //If user account is registered prefs.edit().putBoolean(AppConstants.KEY_OMS_IS_WORKING, false).commit(); - if(!JobUtils.isScannerJobRegistered(context)) { + if (!JobUtils.isScannerJobRegistered(context)) { + //scanner job isn't registered then register it + JobUtils.scheduleScannerJob(context); + } + } + } else { + + Account mAccount = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), AccountManager.get(context)); + + if (mAccount != null) { + + String accountName = mAccount.name; + String accountType = mAccount.type; + + //If data come from intent, store them into pref because there aren't stored + prefs.edit().putString(AccountManager.KEY_ACCOUNT_NAME, accountName) + .putBoolean(AppConstants.KEY_OMS_IS_WORKING, false) + .putString(AccountManager.KEY_ACCOUNT_TYPE, accountType) + .apply(); + + if (!JobUtils.isScannerJobRegistered(context)) { //scanner job isn't registered then register it - JobUtils.scheduleScannerJob(context);} + JobUtils.scheduleScannerJob(context); + } } } } diff --git a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java index 9dc2630b..c821bcf9 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -24,13 +24,17 @@ import android.support.annotation.NonNull; import android.support.v4.app.NotificationCompat; import android.util.Log; import android.webkit.MimeTypeMap; + import com.owncloud.android.lib.common.OwnCloudBasicCredentials; import com.owncloud.android.lib.common.OwnCloudClient; import com.owncloud.android.lib.common.OwnCloudClientFactory; import com.owncloud.android.lib.common.accounts.AccountUtils; import com.owncloud.android.lib.resources.files.FileUtils; + import java.io.File; + import foundation.e.drive.receivers.ScreenOffReceiver; + import static foundation.e.drive.utils.AppConstants.MEDIASYNC_PROVIDER_AUTHORITY; import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORITY; @@ -38,54 +42,57 @@ import static foundation.e.drive.utils.AppConstants.SETTINGSYNC_PROVIDER_AUTHORI /** * @author Vincent Bourgmayer */ -public abstract class CommonUtils { +public abstract class CommonUtils { final private static String TAG = CommonUtils.class.getSimpleName(); /** * Set ServiceUncaughtExceptionHandler to be the MainThread Exception Handler * Or update the service which use it + * * @param service current service */ - public static void setServiceUnCaughtExceptionHandler(Service service){ + public static void setServiceUnCaughtExceptionHandler(Service service) { Thread.UncaughtExceptionHandler defaultUEH = Thread.getDefaultUncaughtExceptionHandler(); - if(defaultUEH.getClass().getSimpleName().equals(ServiceExceptionHandler.class.getSimpleName())){ + if (defaultUEH.getClass().getSimpleName().equals(ServiceExceptionHandler.class.getSimpleName())) { Log.d("ObserverService", "ServiceExceptionHandler already set!"); ((ServiceExceptionHandler) defaultUEH).setService(service); - }else{ + } else { Thread.setDefaultUncaughtExceptionHandler(new ServiceExceptionHandler(service)); } } - /** * Unregister from screeOffReceiver component + * * @param context app context * @return true if unregistration was successful or false if it encounter an exception */ - public static boolean unregisterScreenOff(Context context){ + public static boolean unregisterScreenOff(Context context) { try { Log.d("TAG", "unregisterReceiver(screenOffReceiver)"); context.unregisterReceiver(ScreenOffReceiver.getInstance()); - }catch(IllegalArgumentException e){ + } catch (IllegalArgumentException e) { Log.w(TAG, "Can't unregister screenOffReceiver "); return false; } return true; } + /** * Send notification to UI. - * @dev-only + * * @param context app context - * @param id notification ID - * @param msg Message of the notification - * @param icon Icon to use + * @param id notification ID + * @param msg Message of the notification + * @param icon Icon to use * @param pIntent intent to launch when user tap on the notif. Can be null - * @param ticker The ticker message, shown first. Can be null + * @param ticker The ticker message, shown first. Can be null + * @dev-only */ - public static void sendNotification(Context context,int id, String msg, int icon, PendingIntent pIntent,String ticker) { + public static void sendNotification(Context context, int id, String msg, int icon, PendingIntent pIntent, String ticker) { NotificationCompat.Builder builder = new NotificationCompat.Builder(context) .setSmallIcon(icon) @@ -95,23 +102,41 @@ public abstract class CommonUtils { NotificationManager notifyMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); - if(pIntent != null) builder.setContentIntent(pIntent); - if(ticker != null) builder.setTicker(ticker); + if (pIntent != null) builder.setContentIntent(pIntent); + if (ticker != null) builder.setTicker(ticker); notifyMgr.notify(id, builder.build()); } /** * This method retrieve Account corresponding to account's name and type + * * @param accountName Account Name, shouldn't be null * @param accountType account type - * @param am Account Manager + * @param am Account Manager * @return Account or null if not found */ - public static Account getAccount(@NonNull String accountName, String accountType, @NonNull AccountManager am){ + public static Account getAccount(@NonNull String accountName, String accountType, @NonNull AccountManager am) { Account[] accounts = am.getAccountsByType(accountType); - for(int i = -1, size = accounts.length; ++i < size;){ - if(accounts[i].name.equals( accountName )){ + for (int i = -1, size = accounts.length; ++i < size; ) { + if (accounts[i].name.equals(accountName)) { + return accounts[i]; + } + } + return null; + } + + /** + * This method retrieve Account corresponding to account's type + * + * @param accountType account type + * @param am Account Manager + * @return Account or null if not found + */ + public static Account getAccount(String accountType, @NonNull AccountManager am) { + Account[] accounts = am.getAccounts(); + for (int i = -1, size = accounts.length; ++i < size; ) { + if (accounts[i].type.equals(accountType)) { return accounts[i]; } } @@ -121,29 +146,33 @@ public abstract class CommonUtils { /** * Say if synchronisation is allowed - * @param account Account used for synchronisation + * + * @param account Account used for synchronisation * @param syncedFileStateIsMedia true if the concerned syncedFileState is a media's type element, false if it is a settings's type element * @return */ - public static boolean isThisSyncAllowed(Account account, boolean syncedFileStateIsMedia){ - return ( syncedFileStateIsMedia && isMediaSyncEnabled(account) ) - || ( !syncedFileStateIsMedia && isSettingsSyncEnabled(account) ) ; + public static boolean isThisSyncAllowed(Account account, boolean syncedFileStateIsMedia) { + return (syncedFileStateIsMedia && isMediaSyncEnabled(account)) + || (!syncedFileStateIsMedia && isSettingsSyncEnabled(account)); } /** * Say if Media Sync is enabled in account + * * @param account Concerned account * @return true if media sync enabled */ - public static boolean isMediaSyncEnabled(Account account){ + public static boolean isMediaSyncEnabled(Account account) { return ContentResolver.getSyncAutomatically(account, MEDIASYNC_PROVIDER_AUTHORITY); } + /** * Say if Settings Sync is enabled in account + * * @param account Concerned account * @return true if enabled */ - public static boolean isSettingsSyncEnabled(Account account){ + public static boolean isSettingsSyncEnabled(Account account) { return ContentResolver.getSyncAutomatically(account, SETTINGSYNC_PROVIDER_AUTHORITY); } @@ -151,7 +180,7 @@ public abstract class CommonUtils { * @param context app context * @return boolean true if Task could have been launch */ - public static OwnCloudClient getOwnCloudClient( Account account, Context context) { + public static OwnCloudClient getOwnCloudClient(Account account, Context context) { Log.i(TAG, "getOwnCloudClient()"); Uri serverUri; @@ -159,10 +188,10 @@ public abstract class CommonUtils { try { serverUri = Uri.parse(AccountUtils.getBaseUrlForAccount(context, account)); oc = OwnCloudClientFactory.createOwnCloudClient(serverUri, context, true); - oc.setCredentials(new OwnCloudBasicCredentials(account.name, AccountManager.get(context).getPassword(account) ) ); + oc.setCredentials(new OwnCloudBasicCredentials(account.name, AccountManager.get(context).getPassword(account))); - }catch (Exception e){ - Log.e(TAG, "Can\'t parse serverPath to Uri : "+e.toString() ); + } catch (Exception e) { + Log.e(TAG, "Can\'t parse serverPath to Uri : " + e.toString()); oc = null; } return oc; @@ -172,15 +201,16 @@ public abstract class CommonUtils { /** * Return name of a file from its access path + * * @param path File name will be extracted from this path. Do not provide directory path * @return String, the last part after separator of path or null if invalid path has been provided */ public static String getFileNameFromPath(String path) { - try{ + try { String[] splittedString = path.split(FileUtils.PATH_SEPARATOR); return splittedString[splittedString.length - 1]; - }catch(Exception e){ - Log.e(TAG, e.toString() ); + } catch (Exception e) { + Log.e(TAG, e.toString()); return null; } } @@ -235,15 +265,15 @@ public abstract class CommonUtils { public static void doActionMediaScannerConnexionScanFile(Context context, final String filePath) { Log.i(TAG, "doActionMediaScannerConnexionScanFile(" + filePath + ")"); MediaScannerConnection.scanFile(context, - new String[]{filePath}, - new String[]{getMimeType(new File(filePath))}, - new MediaScannerConnection.OnScanCompletedListener() { - @Override - public void onScanCompleted(String path, Uri uri) { - Log.v("MediaScanWork", "file " + path - + " was scanned successfully: " + uri); - } - }); + new String[]{filePath}, + new String[]{getMimeType(new File(filePath))}, + new MediaScannerConnection.OnScanCompletedListener() { + @Override + public void onScanCompleted(String path, Uri uri) { + Log.v("MediaScanWork", "file " + path + + " was scanned successfully: " + uri); + } + }); } /** @@ -265,22 +295,23 @@ public abstract class CommonUtils { /** * Used for debug - * @dev-only + * * @param f File to debug * @return String showing value of file's properties + * @dev-only */ public static String debugFile(File f) { - return "File name: "+f.getName() - +"\n File exist?: "+f.exists() - +"\n File absolute path: "+f.getAbsolutePath() - +"\n File path: "+f.getPath() - +"\n File length: "+f.length() - +"\n File last modif: "+f.lastModified() - +"\n File readable?: "+f.canRead() - +"\n File writable?: "+f.canWrite() - +"\n File is file?: "+f.isFile() - +"\n File is Dir?: "+f.isDirectory() - +"\n File can be read?: "+f.canRead() - +"\n File can be written?: "+f.canWrite(); + return "File name: " + f.getName() + + "\n File exist?: " + f.exists() + + "\n File absolute path: " + f.getAbsolutePath() + + "\n File path: " + f.getPath() + + "\n File length: " + f.length() + + "\n File last modif: " + f.lastModified() + + "\n File readable?: " + f.canRead() + + "\n File writable?: " + f.canWrite() + + "\n File is file?: " + f.isFile() + + "\n File is Dir?: " + f.isDirectory() + + "\n File can be read?: " + f.canRead() + + "\n File can be written?: " + f.canWrite(); } } \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 6a0afdbd..ad5739ee 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,3 +1,4 @@ /e/ Drive + e.foundation.webdav.eelo -- GitLab