From 1a940193f50a85b0c3b568038fd9affcba153d98 Mon Sep 17 00:00:00 2001 From: TheScarastic Date: Wed, 11 May 2022 16:20:37 +0530 Subject: [PATCH] eDrive: fix probable npe and other crashes --- .../e/drive/activity/AccountsActivity.java | 36 ++++++++--- .../e/drive/widgets/EDriveWidget.java | 64 +++++++++++++------ 2 files changed, 73 insertions(+), 27 deletions(-) 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 73f24429..85e858d9 100644 --- a/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java +++ b/app/src/main/java/foundation/e/drive/activity/AccountsActivity.java @@ -24,6 +24,7 @@ import android.content.ComponentName; import android.content.Intent; import android.net.Uri; import android.os.Bundle; +import android.util.Log; import android.view.View; import androidx.appcompat.app.AppCompatActivity; @@ -38,6 +39,7 @@ import foundation.e.drive.utils.CommonUtils; import foundation.e.drive.widgets.EDriveWidget; public class AccountsActivity extends AppCompatActivity { + private static final String TAG = AccountsActivity.class.getSimpleName(); public static final String NON_OFFICIAL_AVATAR_PATH = "/index.php/avatar/"; private static final String ACCOUNT_SETTINGS = @@ -73,19 +75,38 @@ public class AccountsActivity extends AppCompatActivity { 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); + String name = accountManager.getUserData(account, ACCOUNT_DATA_NAME); - binding.name.setText(accountManager.getUserData(account, ACCOUNT_DATA_NAME)); + // For some reason if we cant get name use email as name + if (name == null || name.isEmpty()) { + name = email; + } + + binding.name.setText(name); binding.email.setText(email); binding.progress.setMax(convertIntoMB(totalQuota)); binding.progress.setProgress(convertIntoMB(usedQuota)); binding.progress.setVisibility(View.VISIBLE); - final String totalShownQuota; - if (Long.parseLong(totalQuota) > 0) { - totalShownQuota = CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)); - } else { - totalShownQuota = "?"; + String totalShownQuota = "?"; + String usedShownQuota = "?"; + try { + final long totalQuotaLong = Long.parseLong(totalQuota); + if (totalQuotaLong > 0) { + totalShownQuota = CommonUtils.humanReadableByteCountBin(totalQuotaLong); + } + } catch (NumberFormatException ignored) { + Log.i(TAG, "Bad totalQuotaLong " + totalQuota); + } + + try { + final long usedQuotaLong = Long.parseLong(usedQuota); + if (usedQuotaLong > 0) { + usedShownQuota = CommonUtils.humanReadableByteCountBin(usedQuotaLong); + } + } catch (NumberFormatException ignore) { + Log.i(TAG, "Bad usedQuotaLong " + usedQuota); } binding.plan.setText(getString(R.string.free_plan, totalShownQuota)); @@ -102,8 +123,7 @@ public class AccountsActivity extends AppCompatActivity { binding.myPlan.setVisibility(View.VISIBLE); binding.plan.setVisibility(View.VISIBLE); - binding.status.setText(getString(R.string.progress_status, - CommonUtils.humanReadableByteCountBin(Long.parseLong(usedQuota)), totalShownQuota)); + binding.status.setText(getString(R.string.progress_status, usedShownQuota, 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/widgets/EDriveWidget.java b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java index 34b8f719..56beb8f2 100644 --- a/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java +++ b/app/src/main/java/foundation/e/drive/widgets/EDriveWidget.java @@ -28,6 +28,7 @@ import android.net.Network; import android.net.Uri; import android.os.Bundle; import android.provider.Settings; +import android.util.Log; import android.view.View; import android.widget.RemoteViews; @@ -43,6 +44,8 @@ import foundation.e.drive.utils.CommonUtils; * Implementation of App Widget functionality. */ public class EDriveWidget extends AppWidgetProvider { + private static final String TAG = EDriveWidget.class.getSimpleName(); + public static final String WEBPAGE = "https://esolutions.shop/ecloud-subscriptions/?username=%s&token=placeholder¤t-quota=%s&from=wp"; public static final String ACCOUNT_MANAGER_PACKAGE_NAME = "foundation.e.accountmanager"; @@ -56,15 +59,14 @@ public class EDriveWidget extends AppWidgetProvider { private static final String SHOW_ALIAS = "show_alias"; private static final String HIDE_ALIAS = "hide_alias"; private static final String COPY_ALIAS = "copy_alias"; + private static final String DARK_TEXT_KEY = "foundation.e.blisslauncher.WIDGET_OPTION_DARK_TEXT"; private static boolean showAlias = false; private static boolean isNetworkAvailable = false; + private static boolean isDarkText = false; private final Calendar calender = Calendar.getInstance(); private final SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault()); private RemoteViews views; private Account account = null; - private static final String DARK_TEXT_KEY = "foundation.e.blisslauncher.WIDGET_OPTION_DARK_TEXT"; - private static boolean isDarkText = false; - public static String dataForWeb(Long bytes) { final String space = CommonUtils.humanReadableByteCountBin(bytes); @@ -83,7 +85,12 @@ public class EDriveWidget extends AppWidgetProvider { } public static int convertIntoMB(String quota) { - return (int) (Long.parseLong(quota) / 1048576); // 1024.0 * 1024.0 = 1048576.0 + int convertedData = 0; + try { + convertedData = (int) (Long.parseLong(quota) / (1024 * 1024)); + } catch (NumberFormatException ignore) { + } + return convertedData; } public void updateAppWidget(final Context context) { @@ -188,6 +195,7 @@ public class EDriveWidget extends AppWidgetProvider { 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); + String name = accountManager.getUserData(account, ACCOUNT_DATA_NAME); if (email == null || email.trim().isEmpty()) { noAccountView(context); @@ -199,15 +207,33 @@ public class EDriveWidget extends AppWidgetProvider { views = new RemoteViews(context.getPackageName(), R.layout.e_drive_widget); } views.setTextViewText(R.id.email, email); - views.setTextViewText(R.id.name, accountManager.getUserData(account, ACCOUNT_DATA_NAME)); + + // For some reason if we cant get name use email as name + if (name == null || name.isEmpty()) { + name = email; + } + views.setTextViewText(R.id.name, name); views.setProgressBar(R.id.progress, convertIntoMB(totalQuota), convertIntoMB(usedQuota), false); - final String totalShownQuota; - if (Long.parseLong(totalQuota) > 0) { - totalShownQuota = CommonUtils.humanReadableByteCountBin(Long.parseLong(totalQuota)); - } else { - totalShownQuota = "?"; + String totalShownQuota = "?"; + String usedShownQuota = "?"; + try { + final long totalQuotaLong = Long.parseLong(totalQuota); + if (totalQuotaLong > 0) { + totalShownQuota = CommonUtils.humanReadableByteCountBin(totalQuotaLong); + } + } catch (NumberFormatException ignored) { + Log.i(TAG, "Bad totalQuotaLong " + totalQuota); + } + + try { + final long usedQuotaLong = Long.parseLong(usedQuota); + if (usedQuotaLong > 0) { + usedShownQuota = CommonUtils.humanReadableByteCountBin(usedQuotaLong); + } + } catch (NumberFormatException ignore) { + Log.i(TAG, "Bad usedQuotaLong " + usedQuota); } views.setTextViewText(R.id.planName, context.getString(R.string.free_plan, totalShownQuota)); @@ -222,13 +248,13 @@ public class EDriveWidget extends AppWidgetProvider { } views.setTextViewText(R.id.status, context.getString(R.string.progress_status, - CommonUtils.humanReadableByteCountBin(Long.parseLong(usedQuota)), - totalShownQuota)); + usedShownQuota, totalShownQuota)); + views.setTextViewText(R.id.sync, context.getString(R.string.last_synced, sdf.format(calender.getTime()))); String aliases = accountManager.getUserData(account, ACCOUNT_DATA_ALIAS_KEY); - if (aliases.isEmpty()) { + if (aliases == null || aliases.isEmpty()) { views.setViewVisibility(R.id.show_alias, View.GONE); views.setViewVisibility(R.id.alias1_container, View.GONE); views.setViewVisibility(R.id.hide_alias, View.GONE); @@ -294,10 +320,10 @@ public class EDriveWidget extends AppWidgetProvider { }); } - @Override - public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { - super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions); - isDarkText = newOptions.getBoolean(DARK_TEXT_KEY); - updateAppWidget(context); - } + @Override + public void onAppWidgetOptionsChanged(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Bundle newOptions) { + super.onAppWidgetOptionsChanged(context, appWidgetManager, appWidgetId, newOptions); + isDarkText = newOptions.getBoolean(DARK_TEXT_KEY); + updateAppWidget(context); + } } \ No newline at end of file -- GitLab