diff --git a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java index d0c2fdbf52dafa1f6a01612d902b3ef653b0d2c8..73f24429675cb60cb22f8855915f7996be3c5d36 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -81,8 +81,14 @@ public class AccountsActivity extends AppCompatActivity { binding.progress.setProgress(convertIntoMB(usedQuota)); binding.progress.setVisibility(View.VISIBLE); - binding.plan.setText(getString(R.string.free_plan, - CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)))); + final String totalShownQuota; + if (Long.parseLong(totalQuota) > 0) { + totalShownQuota = CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)); + } else { + totalShownQuota = "?"; + } + + binding.plan.setText(getString(R.string.free_plan, totalShownQuota)); String[] groups = accountManager.getUserData(account, ACCOUNT_DATA_GROUPS).split(","); for (String group : groups) { @@ -97,8 +103,7 @@ public class AccountsActivity extends AppCompatActivity { binding.plan.setVisibility(View.VISIBLE); binding.status.setText(getString(R.string.progress_status, - CommonUtils.humanReadableByteCountBin(Long.parseLong(usedQuota)), - CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)))); + CommonUtils.humanReadableByteCountBin(Long.parseLong(usedQuota)), totalShownQuota)); String aliases = accountManager.getUserData(account, ACCOUNT_DATA_ALIAS_KEY); if (aliases != null && !aliases.isEmpty()) { diff --git a/app/src/main/java/foundation/e/drive/services/InitializerService.java b/app/src/main/java/foundation/e/drive/services/InitializerService.java index fdb73abc13b31e71c6e52a35349d6c60e6038806..efddf95069dc9e5b1c31f9f43d00dea6a20fe9d3 100644 --- a/app/src/main/java/foundation/e/drive/services/InitializerService.java +++ b/app/src/main/java/foundation/e/drive/services/InitializerService.java @@ -90,6 +90,7 @@ public class InitializerService extends Service { } } } + CommonUtils.registerPeriodicUserInfoChecking(WorkManager.getInstance(this)); return super.onStartCommand(intent, flags, startId); } @@ -100,8 +101,6 @@ public class InitializerService extends Service { return; } - CommonUtils.registerPeriodicUserInfoChecking(WorkManager.getInstance(this)); - final List syncCategories = new ArrayList<>(); syncCategories.addAll(Arrays.asList(MEDIA_SYNCABLE_CATEGORIES)); 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 f2476841f5833d6ef1ac4e40bfdf754762a110f4..a9e5f2923473c2a64d4387b8ab7d36d8f754a5d4 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -23,6 +23,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.provider.Settings; import android.view.View; @@ -54,10 +56,9 @@ public class EDriveWidget extends AppWidgetProvider { private static final String HIDE_ALIAS = "hide_alias"; private static final String COPY_ALIAS = "copy_alias"; private static boolean showAlias = false; - + private static boolean isNetworkAvailable = false; private final Calendar calender = Calendar.getInstance(); private final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault()); - private RemoteViews views; private Account account = null; @@ -101,7 +102,7 @@ public class EDriveWidget extends AppWidgetProvider { accountManager); } - if (account == null && CommonUtils.haveNetworkConnection(context, true)) { + if (account == null) { noAccountView(context); } else { onAccountAvailable(context, accountManager); @@ -112,6 +113,7 @@ public class EDriveWidget extends AppWidgetProvider { @Override public void onEnabled(Context context) { super.onEnabled(context); + registerConnectivityCallback(context); updateAppWidget(context); } @@ -141,6 +143,11 @@ public class EDriveWidget extends AppWidgetProvider { } private void noAccountView(Context context) { + if (!isNetworkAvailable) { + noInternetView(context); + return; + } + views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget_login); Intent accountIntent = buildIntent("", "") .setComponent(new ComponentName(ACCOUNT_MANAGER_PACKAGE_NAME, @@ -156,15 +163,21 @@ public class EDriveWidget extends AppWidgetProvider { views.setTextViewText(R.id.summary, context.getString(R.string.login_summary)); } + private void noInternetView(Context context) { + if (!isNetworkAvailable) { + views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget_login); + views.setViewVisibility(R.id.button_container, View.GONE); + views.setTextViewText(R.id.summary, context.getString(R.string.no_internet_widget)); + } + } + private void onAccountAvailable(Context context, AccountManager accountManager) { final String usedQuota = accountManager.getUserData(account, ACCOUNT_DATA_USED_QUOTA_KEY); final String totalQuota = accountManager.getUserData(account, ACCOUNT_DATA_TOTAL_QUOTA_KEY); final String email = accountManager.getUserData(account, ACCOUNT_DATA_EMAIL); if (email == null || email.trim().isEmpty()) { - views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget_login); - views.setViewVisibility(R.id.button_container, View.GONE); - views.setTextViewText(R.id.summary, context.getString(R.string.no_internet_widget)); + noAccountView(context); return; } @@ -175,8 +188,14 @@ public class EDriveWidget extends AppWidgetProvider { views.setProgressBar(R.id.progress, convertIntoMB(totalQuota), convertIntoMB(usedQuota), false); - views.setTextViewText(R.id.planName, context.getString(R.string.free_plan, - CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)))); + final String totalShownQuota; + if (Long.parseLong(totalQuota) > 0) { + totalShownQuota = CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)); + } else { + totalShownQuota = "?"; + } + + views.setTextViewText(R.id.planName, context.getString(R.string.free_plan, totalShownQuota)); String[] groups = accountManager.getUserData(account, ACCOUNT_DATA_GROUPS).split(","); for (String group : groups) { @@ -189,7 +208,7 @@ public class EDriveWidget extends AppWidgetProvider { views.setTextViewText(R.id.status, context.getString(R.string.progress_status, CommonUtils.humanReadableByteCountBin(Long.parseLong(usedQuota)), - CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)))); + totalShownQuota)); views.setTextViewText(R.id.sync, context.getString(R.string.last_synced, sdf.format(calender.getTime()))); @@ -237,4 +256,26 @@ 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/java/foundation/e/drive/work/AccountUserInfoWorker.java b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java index 7690fd5808e56f72d493b2cdbe0a3d277d03e072..b253289484acf2402dc7b9d97413f049d760a5f4 100644 --- a/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java +++ b/app/src/main/java/foundation/e/drive/work/AccountUserInfoWorker.java @@ -97,7 +97,10 @@ public class AccountUserInfoWorker extends Worker { name = userInfo.displayName; } final double relativeQuota = userInfo.getQuota().relative; - final long totalQuota = userInfo.getQuota().total; + long totalQuota = userInfo.getQuota().total; + if (totalQuota <= 0) { + totalQuota = 0; + } final long usedQuota = userInfo.getQuota().used; final String groups = String.join(",", userInfo.groups); final String email = userInfo.id; diff --git a/app/src/main/res/xml/e_drive_widget_info.xml b/app/src/main/res/xml/e_drive_widget_info.xml index dd3cef9c52ef95390f43f0fa0441dfb332e85f6e..acf7281af816cfca6d704a383ef7fece0f24ea34 100644 --- a/app/src/main/res/xml/e_drive_widget_info.xml +++ b/app/src/main/res/xml/e_drive_widget_info.xml @@ -5,5 +5,5 @@ android:initialLayout="@layout/e_drive_widget_login" android:minWidth="250dp" android:minHeight="250dp" - android:updatePeriodMillis="1800000" + android:updatePeriodMillis="0" android:widgetCategory="home_screen" /> \ No newline at end of file