From b54e9aabb1b869b98c351705c06a1f91d5f0563a Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Fri, 2 Apr 2021 17:35:07 +0800 Subject: [PATCH] New option: delete crash reports in CrashReportsFragment. --- .../infinityforreddit/AppComponent.java | 3 + .../activities/LoginActivity.java | 1 - .../fragments/ViewPostDetailFragment.java | 2 - .../settings/CrashReportsFragment.java | 57 +++++++++++++++++-- .../main/res/menu/crash_reports_fragment.xml | 10 ++++ app/src/main/res/values/strings.xml | 3 + 6 files changed, 68 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/menu/crash_reports_fragment.xml diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java index 4af5a6c7..8ac039ea 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/AppComponent.java @@ -73,6 +73,7 @@ import ml.docilealligator.infinityforreddit.services.DownloadMediaService; import ml.docilealligator.infinityforreddit.services.DownloadRedditVideoService; import ml.docilealligator.infinityforreddit.services.SubmitPostService; import ml.docilealligator.infinityforreddit.settings.AdvancedPreferenceFragment; +import ml.docilealligator.infinityforreddit.settings.CrashReportsFragment; import ml.docilealligator.infinityforreddit.settings.CustomizeBottomAppBarFragment; import ml.docilealligator.infinityforreddit.settings.CustomizeMainPageTabsFragment; import ml.docilealligator.infinityforreddit.settings.DownloadLocationPreferenceFragment; @@ -257,4 +258,6 @@ public interface AppComponent { void inject(SuicidePreventionActivity suicidePreventionActivity); void inject(WebViewActivity webViewActivity); + + void inject(CrashReportsFragment crashReportsFragment); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java index 2eb2de1d..21f49973 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/LoginActivity.java @@ -95,7 +95,6 @@ public class LoginActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { ((Infinity) getApplication()).getAppComponent().inject(this); - int llll = 0 / 0; setImmersiveModeNotApplicable(); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java index 91b82fdf..5f41dc1d 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/fragments/ViewPostDetailFragment.java @@ -5,7 +5,6 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; @@ -270,7 +269,6 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic } mGlide = Glide.with(this); - Resources resources = getResources(); mLocale = getResources().getConfiguration().locale; mLinearLayoutManager = new LinearLayoutManager(activity); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CrashReportsFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CrashReportsFragment.java index 6fee1427..350addf8 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CrashReportsFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/settings/CrashReportsFragment.java @@ -1,29 +1,38 @@ package ml.docilealligator.infinityforreddit.settings; +import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; +import android.graphics.drawable.Drawable; import android.os.Bundle; import android.view.LayoutInflater; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.appcompat.view.menu.MenuItemImpl; +import androidx.core.graphics.drawable.DrawableCompat; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; import com.crazylegend.crashyreporter.CrashyReporter; +import javax.inject.Inject; + +import ml.docilealligator.infinityforreddit.Infinity; import ml.docilealligator.infinityforreddit.R; import ml.docilealligator.infinityforreddit.adapters.CrashReportsRecyclerViewAdapter; +import ml.docilealligator.infinityforreddit.customtheme.CustomThemeWrapper; -/** - * A simple {@link Fragment} subclass. - * Use the {@link CrashReportsFragment#newInstance} factory method to - * create an instance of this fragment. - */ public class CrashReportsFragment extends Fragment { + @Inject + CustomThemeWrapper mCustomThemeWrapper; private Activity activity; public CrashReportsFragment() { @@ -36,12 +45,50 @@ public class CrashReportsFragment extends Fragment { // Inflate the layout for this fragment RecyclerView recyclerView = (RecyclerView) inflater.inflate(R.layout.fragment_crash_reports, container, false); + ((Infinity) activity.getApplication()).getAppComponent().inject(this); + + setHasOptionsMenu(true); + recyclerView.setLayoutManager(new LinearLayoutManager(activity)); recyclerView.setAdapter(new CrashReportsRecyclerViewAdapter(CrashyReporter.INSTANCE.getLogsAsStrings())); return recyclerView; } + @Override + public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) { + inflater.inflate(R.menu.crash_reports_fragment, menu); + applyMenuItemTheme(menu); + } + + @Override + public boolean onOptionsItemSelected(@NonNull MenuItem item) { + if (item.getItemId() == R.id.action_delete_logs_crash_reports_fragment) { + CrashyReporter.INSTANCE.purgeLogs(); + Toast.makeText(activity, R.string.crash_reports_deleted, Toast.LENGTH_SHORT).show(); + return true; + } + return false; + } + + @SuppressLint("RestrictedApi") + protected boolean applyMenuItemTheme(Menu menu) { + if (mCustomThemeWrapper != null) { + int size = Math.min(menu.size(), 2); + for (int i = 0; i < size; i++) { + MenuItem item = menu.getItem(i); + if (((MenuItemImpl) item).requestsActionButton()) { + Drawable drawable = item.getIcon(); + if (drawable != null) { + DrawableCompat.setTint(drawable, mCustomThemeWrapper.getToolbarPrimaryTextAndIconColor()); + item.setIcon(drawable); + } + } + } + } + return true; + } + @Override public void onAttach(@NonNull Context context) { super.onAttach(context); diff --git a/app/src/main/res/menu/crash_reports_fragment.xml b/app/src/main/res/menu/crash_reports_fragment.xml new file mode 100644 index 00000000..67bc62b5 --- /dev/null +++ b/app/src/main/res/menu/crash_reports_fragment.xml @@ -0,0 +1,10 @@ + + + + \ 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 496b787f..3fe9b283 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -82,6 +82,7 @@ Share Link Copy Link Add to Post Filter + Delete Logs Error occurred when parsing the JSON response Error Retrieving the token @@ -1094,4 +1095,6 @@ Suggest Title Failed to suggest a title + Crash reports are deleted +