From fc713e2e4d1e5701a4f69605dd6cac03e861be63 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Thu, 14 Apr 2022 19:26:23 +0530 Subject: [PATCH 1/3] eDrive: widget: Properly support no internet case --- .../e/drive/widgets/EDriveWidget.java | 88 ++++++++++++++----- .../main/res/layout/e_drive_widget_login.xml | 9 +- app/src/main/res/values/strings.xml | 1 + 3 files changed, 70 insertions(+), 28 deletions(-) diff --git a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java index 229850c6..eb76f33c 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -16,6 +16,8 @@ import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; +import android.net.ConnectivityManager; +import android.net.Network; import android.net.Uri; import android.os.Handler; import android.os.HandlerThread; @@ -56,14 +58,19 @@ public class EDriveWidget extends AppWidgetProvider { private static final String COPY_ALIAS = "copy_alias"; private static boolean showAlias = false; + private static boolean isNetworkAvailable = false; + + private static UserInfo userInfo = null; + private static ArrayList aliases = new ArrayList<>(); private final Calendar calender = Calendar.getInstance(); private final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault()); private final GetRemoteUserInfoOperation getRemoteUserInfoOperation = new GetRemoteUserInfoOperation(); private final GetAliasOperation getAliasOperation = new GetAliasOperation(); - private ArrayList aliases = new ArrayList<>(); - private UserInfo userInfo = null; - private RemoteViews views = null; + + private OwnCloudClient client = null; + private RemoteViews views; + public static String dataForWeb(Long bytes) { final String space = CommonUtils.humanReadableByteCountBin(bytes); @@ -100,9 +107,14 @@ public class EDriveWidget extends AppWidgetProvider { final AccountManager accountManager = AccountManager.get(context); final Account account = CommonUtils.getAccount(context.getString(R.string.eelo_account_type), accountManager); - final OwnCloudClient client = CommonUtils.getOwnCloudClient(account, context); - views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget); + if (account == null || isNetworkAvailable) { + noAccountView(context); + appWidgetManager.updateAppWidget(appWidgetId, views); + return; + } + + client = CommonUtils.getOwnCloudClient(account, context); final HandlerThread handlerThread = new HandlerThread("Network Request"); handlerThread.start(); final Handler mHandler = new Handler(handlerThread.getLooper()); @@ -110,7 +122,8 @@ public class EDriveWidget extends AppWidgetProvider { final EDriveNetworkCallback callback = new EDriveNetworkCallback() { @Override public void onComplete() { - onNetworkRequestCompleted(context, appWidgetManager, appWidgetId, client, account); + onNetworkRequestCompleted(context); + appWidgetManager.updateAppWidget(appWidgetId, views); } }; @@ -153,11 +166,12 @@ public class EDriveWidget extends AppWidgetProvider { } @Override - public void onReceive(Context context, Intent intent) { + public void onReceive(final Context context, Intent intent) { final String action = intent.getAction(); if (action == null) return; switch (action) { case Intent.ACTION_BOOT_COMPLETED: + registerConnectivityCallback(context); case AccountManager.LOGIN_ACCOUNTS_CHANGED_ACTION: updateAppWidget(context); break; @@ -177,24 +191,32 @@ public class EDriveWidget extends AppWidgetProvider { super.onReceive(context, intent); } - private void onNetworkRequestCompleted(Context context, AppWidgetManager appWidgetManager, - int appWidgetId, OwnCloudClient client, Account account) { - if (account == null || userInfo == null) { + private void noAccountView(Context context) { + views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget_login); + Intent accountIntent = buildIntent("", "") + .setComponent(new ComponentName(ACCOUNT_MANAGER_PACKAGE_NAME, + GET_ACCOUNT_MANAGER_COMPONENT_NAME)) + .putExtra(SETUP_ACCOUNT_PROVIDER_TYPE, ACCOUNT_PROVIDER_EELO); + PendingIntent addAccountIntent = PendingIntent.getActivity(context, 0, + accountIntent, PendingIntent.FLAG_IMMUTABLE); + views.setOnClickPendingIntent(R.id.login, addAccountIntent); + PendingIntent pendingIntentNewAccount = PendingIntent.getActivity(context, 0, + buildIntent(Intent.ACTION_VIEW, ADD_ACCOUNT_WEBPAGE), PendingIntent.FLAG_IMMUTABLE); + views.setOnClickPendingIntent(R.id.newAccount, pendingIntentNewAccount); + views.setViewVisibility(R.id.button_container, View.VISIBLE); + views.setTextViewText(R.id.summary, context.getString(R.string.login_summary)); + } + + private void onNetworkRequestCompleted(Context context) { + if (!isNetworkAvailable && userInfo == null) { views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget_login); - Intent accountIntent = buildIntent("", "") - .setComponent(new ComponentName(ACCOUNT_MANAGER_PACKAGE_NAME, - GET_ACCOUNT_MANAGER_COMPONENT_NAME)) - .putExtra(SETUP_ACCOUNT_PROVIDER_TYPE, ACCOUNT_PROVIDER_EELO); - PendingIntent addAccountIntent = PendingIntent.getActivity(context, 0, - accountIntent, PendingIntent.FLAG_IMMUTABLE); - views.setOnClickPendingIntent(R.id.login, addAccountIntent); - PendingIntent pendingIntentNewAccount = PendingIntent.getActivity(context, 0, - buildIntent(Intent.ACTION_VIEW, ADD_ACCOUNT_WEBPAGE), PendingIntent.FLAG_IMMUTABLE); - views.setOnClickPendingIntent(R.id.newAccount, pendingIntentNewAccount); - appWidgetManager.updateAppWidget(appWidgetId, views); + views.setViewVisibility(R.id.button_container, View.GONE); + views.setTextViewText(R.id.summary, context.getString(R.string.no_internet_widget)); return; } + views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget); + // Construct the RemoteViews object final int usedMB = convertIntoMB(userInfo.quota.used); final int totalMB = convertIntoMB(userInfo.quota.total); @@ -257,9 +279,6 @@ public class EDriveWidget extends AppWidgetProvider { client.getCredentials().getAuthToken(), dataForWeb(userInfo.quota.total))), PendingIntent.FLAG_IMMUTABLE); views.setOnClickPendingIntent(R.id.upgrade, pendingIntentUpgrade); - - // Instruct the widget manager to update the widget - appWidgetManager.updateAppWidget(appWidgetId, views); } private PendingIntent getPendingSelfIntent(Context context, String action, String data) { @@ -271,4 +290,25 @@ public class EDriveWidget extends AppWidgetProvider { return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_IMMUTABLE); } + private void registerConnectivityCallback(final Context context) { + ConnectivityManager cm = (ConnectivityManager) context.getSystemService( + Context.CONNECTIVITY_SERVICE); + cm.registerDefaultNetworkCallback(new ConnectivityManager.NetworkCallback() { + @Override + public void onAvailable(Network network) { + if (!isNetworkAvailable) { + isNetworkAvailable = true; + updateAppWidget(context); + } + } + + @Override + public void onLost(Network network) { + if (isNetworkAvailable) { + isNetworkAvailable = false; + updateAppWidget(context); + } + } + }); + } } \ No newline at end of file diff --git a/app/src/main/res/layout/e_drive_widget_login.xml b/app/src/main/res/layout/e_drive_widget_login.xml index 393572e5..be8b62f0 100644 --- a/app/src/main/res/layout/e_drive_widget_login.xml +++ b/app/src/main/res/layout/e_drive_widget_login.xml @@ -21,15 +21,16 @@ android:layout_height="wrap_content" android:layout_alignTop="@id/account" android:layout_marginTop="30dp" - android:gravity="center" + android:layout_marginBottom="50dp" android:drawableTop="@drawable/ic_cloud" - android:textFontWeight="400" android:drawablePadding="10dp" - android:textColor="@color/widget_text_color2" + android:gravity="center" android:text="@string/login_summary" - android:layout_marginBottom="50dp"/> + android:textColor="@color/widget_text_color2" + android:textFontWeight="400" /> Hide aliases alias Aliases + No internet connection detected, widget will auto update upon active internet connection eDrive\'s notification eDrive\'s notification channel -- GitLab From 5dd49983ade1675283aa9e21d739fe9bff293f88 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Fri, 15 Apr 2022 11:25:54 +0530 Subject: [PATCH 2/3] eDrive: Fix typo in widget name --- app/src/main/res/values/strings.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 0fccf4b0..3f5d572d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,7 +4,7 @@ Add widget - My cloud Account + My cloud account %1$s Free Plan Premium %1$s Plan %1$s OF %2$s USED -- GitLab From 969228ad7864e250d27dd0bf2515eae0e30adb4d Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Fri, 15 Apr 2022 11:45:01 +0530 Subject: [PATCH 3/3] eDrive: Use English locale to format data size some locale changes decimal operator from "." to "," this causing crash while parsing them as double so make it default to english --- app/src/main/java/foundation/e/drive/utils/CommonUtils.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 72484925..b93257c4 100644 --- a/app/src/main/java/foundation/e/drive/utils/CommonUtils.java +++ b/app/src/main/java/foundation/e/drive/utils/CommonUtils.java @@ -341,7 +341,7 @@ public abstract class CommonUtils { ci.next(); } value *= Long.signum(bytes); - return String.format(Locale.getDefault(), "%.1f %cB", value / 1024.0, ci.current()); + return String.format(Locale.ENGLISH, "%.1f %cB", value / 1024.0, ci.current()); } /** * Enqueue a unique periodic worker to look for file to be synchronized (remote files + local files -- GitLab