From 726632a0f2e37dff5ba3ea3d30a5eeb674f60790 Mon Sep 17 00:00:00 2001 From: Martin Zeitler Date: Fri, 11 Oct 2024 23:42:06 +0200 Subject: [PATCH] some code cleanup. --- README.md | 2 +- build.gradle | 14 ++++---- gradle/libs.versions.toml | 16 ++++++--- library/build.gradle | 15 +++++---- mobile/build.gradle | 15 +++++---- .../java/io/syslogic/github/Constants.java | 3 ++ .../github/content/RepositorySyncAdapter.java | 10 ++++-- .../github/dialog/BaseDialogFragment.java | 8 ++--- .../github/fragment/HomeScreenFragment.java | 8 ++--- .../fragment/RepositorySearchFragment.java | 33 ++++++++++++------- .../syslogic/github/network/TokenHelper.java | 32 +++++++++--------- .../ic_baseline_bookmark_add_48.xml | 13 ++++++++ .../ic_baseline_bookmarks_48.xml | 8 +++-- .../ic_baseline_cloud_download_24.xml | 2 ++ ..._24.xml => ic_baseline_folder_copy_24.xml} | 0 .../drawable/ic_baseline_bookmark_add_48.xml | 16 ++++++--- .../res/drawable/ic_baseline_bookmarks_48.xml | 8 +++-- .../ic_baseline_cloud_download_24.xml | 2 ++ ..._24.xml => ic_baseline_folder_copy_24.xml} | 3 ++ .../src/main/res/layout/toolbar_download.xml | 2 +- version.properties | 2 -- 21 files changed, 135 insertions(+), 77 deletions(-) create mode 100644 mobile/src/main/res/drawable-night/ic_baseline_bookmark_add_48.xml rename mobile/src/main/res/drawable-night/{baseline_folder_copy_24.xml => ic_baseline_folder_copy_24.xml} (100%) rename mobile/src/main/res/drawable/{baseline_folder_copy_24.xml => ic_baseline_folder_copy_24.xml} (99%) delete mode 100644 version.properties diff --git a/README.md b/README.md index 0af2315a..1803e06c 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ The JitPack repository URL would be: `maven { url 'https://jitpack.io' }` dependencies { // implementation "io.syslogic:androidx-github:master-SNAPSHOT" - implementation "io.syslogic:androidx-github:1.1.8" + implementation "io.syslogic:androidx-github:1.1.9" } [![Release](https://jitpack.io/v/syslogic/androidx-github.svg)](https://jitpack.io/#io.syslogic/androidx-github) diff --git a/build.gradle b/build.gradle index 841ce276..d4546fb5 100644 --- a/build.gradle +++ b/build.gradle @@ -5,12 +5,6 @@ plugins { alias(libs.plugins.androidx.navigation.safeargs) apply false } -/** Version Settings, loaded from file `version.properties` */ -def version = new Properties() -version.load(new FileInputStream(rootProject.file('version.properties'))) -project.ext.set('versionCode', new Integer(version['versionCode'])) -project.ext.set('versionName', version['versionName']) - /* JitPack: use tag as versionName. */ if (System.env.JITPACK) { project.ext.set('version_name', System.env.VERSION) @@ -22,13 +16,15 @@ project.ext.set('archiveBuildTypes', ['debug', 'release']) /** Keystore Settings, loaded from keystore.properties */ if (rootProject.file('keystore.properties').exists()) { def keystore = new Properties() - keystore.load(new FileInputStream(rootProject.file('keystore.properties'))) + def ins = new FileInputStream(rootProject.file('keystore.properties')) + keystore.load(ins) project.ext.set('debugKeystorePass', keystore['debugKeystorePass']) project.ext.set('debugKeyAlias', keystore['debugKeyAlias']) project.ext.set('debugKeyPass', keystore['debugKeyPass']) project.ext.set('releaseKeystorePass', keystore['releaseKeystorePass']) project.ext.set('releaseKeyAlias', keystore['releaseKeyAlias']) project.ext.set('releaseKeyPass', keystore['releaseKeyPass']) + ins.close() } /** Modules */ @@ -40,7 +36,9 @@ allprojects { def requested = details.requested if (requested.group == 'org.jetbrains.kotlin') { List list = ['kotlin-stdlib', 'kotlin-stdlib-jdk7', 'kotlin-stdlib-jdk8', 'kotlin-stdlib-common'] - if (list.contains(requested.name)) { details.useVersion libs.versions.kotlin.get() } + if (list.contains(requested.name)) { + details.useVersion libs.versions.kotlin.get() + } } } } diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6ed37e05..38e675d3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -1,9 +1,17 @@ [versions] -android_gradle_plugin = '8.6.0' +app_version_code = '4' +app_version_name = '1.0.4' +android_build_tools = '35.0.0' +android_compile_sdk = '35' +android_target_sdk = '35' +android_min_sdk = '22' + +# Gradle Plugins +android_gradle_plugin = '8.7.0' kotlin = '2.0.20' material_design = '1.12.0' retrofit = '2.11.0' -gson = '2.10.1' +gson = '2.11.0' junit = '4.13.2' flexbox = '3.0.0' # Note: DirCacheCheckout.java uses InputStream.transferTo() since jgit 6.3; no such method on Android. @@ -11,8 +19,8 @@ jgit = '6.2.0.202206071550-r' slf4j = '2.0.13' androidx_appcompat = '1.7.0' androidx_annotation = '1.8.2' -androidx_navigation = '2.8.0' -androidx_fragment = '1.8.3' +androidx_navigation = '2.8.2' +androidx_fragment = '1.8.4' androidx_recyclerview = '1.3.2' androidx_cardview = '1.0.0' androidx_preference = '1.2.1' diff --git a/library/build.gradle b/library/build.gradle index 9a80d45e..e72d280b 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -5,16 +5,17 @@ plugins { } base { - archivesName = 'androidx_github_' + rootProject.ext.get('versionName') + String versionName = libs.versions.app.version.name.get() + archivesName = "androidx_github_$versionName" } android { namespace 'io.syslogic.github.api' - buildToolsVersion = '35.0.0' - compileSdk 35 + buildToolsVersion = libs.versions.android.build.tools.get() + compileSdk Integer.parseInt(libs.versions.android.compile.sdk.get()) defaultConfig { - minSdk 22 - targetSdk 35 + minSdk Integer.parseInt(libs.versions.android.min.sdk.get()) + targetSdk Integer.parseInt(libs.versions.android.target.sdk.get()) testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFile "${project.rootDir}/proguard/consumer.pro" } @@ -165,7 +166,7 @@ artifacts { } group = 'io.syslogic' -version = rootProject.ext.get('versionName') +version = libs.versions.app.version.name.get() afterEvaluate { publishing { @@ -173,7 +174,7 @@ afterEvaluate { release(MavenPublication) { groupId = group artifactId = 'androidx-github' - version = rootProject.ext.get('versionName') + version = libs.versions.app.version.name.get() from components.getByName('release') pom { name = 'GitHub API Client' diff --git a/mobile/build.gradle b/mobile/build.gradle index 51cf4936..3ada45a3 100644 --- a/mobile/build.gradle +++ b/mobile/build.gradle @@ -19,21 +19,22 @@ if (rootProject.file('token.properties').exists()) { } base { - archivesName = 'github_client_' + rootProject.ext.get('versionName') + String versionName = libs.versions.app.version.name.get() + archivesName = "github_client_$versionName" } android { namespace 'io.syslogic.github' - buildToolsVersion = '35.0.0' - compileSdk 35 + buildToolsVersion = libs.versions.android.build.tools.get() + compileSdk Integer.parseInt(libs.versions.android.compile.sdk.get()) defaultConfig { - minSdk 22 - targetSdk 35 + minSdk Integer.parseInt(libs.versions.android.min.sdk.get()) + targetSdk Integer.parseInt(libs.versions.android.target.sdk.get()) + versionCode Integer.parseInt(libs.versions.app.version.code.get()) + versionName libs.versions.app.version.name.get() applicationId 'io.syslogic.github' //noinspection GroovyConstructorNamedArguments manifestPlaceholders = [ accessToken: "" ] - versionName rootProject.ext.get('versionName') - versionCode rootProject.ext.get('versionCode') testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" testBuildType "debug" multiDexEnabled true diff --git a/mobile/src/main/java/io/syslogic/github/Constants.java b/mobile/src/main/java/io/syslogic/github/Constants.java index e70d3bb4..28bc806d 100644 --- a/mobile/src/main/java/io/syslogic/github/Constants.java +++ b/mobile/src/main/java/io/syslogic/github/Constants.java @@ -25,6 +25,9 @@ public final class Constants { /** Deprecated SDK Constant */ @NonNull public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; + /** The default query-string default query-string is being used, when table `query_strings` is empty. */ + @NonNull public static final String DEFAULT_QUERY_STRING = "topic:android"; + /** Table Names */ @NonNull public static final String TABLE_QUERY_STRINGS = "query_strings"; diff --git a/mobile/src/main/java/io/syslogic/github/content/RepositorySyncAdapter.java b/mobile/src/main/java/io/syslogic/github/content/RepositorySyncAdapter.java index 7c5e75b9..d9aaf480 100644 --- a/mobile/src/main/java/io/syslogic/github/content/RepositorySyncAdapter.java +++ b/mobile/src/main/java/io/syslogic/github/content/RepositorySyncAdapter.java @@ -2,6 +2,7 @@ import android.accounts.Account; import android.app.Activity; +import android.app.Application; import android.content.AbstractThreadedSyncAdapter; import android.content.ContentProviderClient; import android.content.Context; @@ -45,7 +46,7 @@ public class RepositorySyncAdapter extends AbstractThreadedSyncAdapter { private final SharedPreferences prefs; - private final String accessToken; + private String accessToken = null; private final String username; @@ -62,7 +63,12 @@ public RepositorySyncAdapter(@NonNull Context context, boolean autoInitialize, b super(context, autoInitialize, allowParallelSyncs); this.prefs = PreferenceManager.getDefaultSharedPreferences(context); this.dao = Abstraction.getInstance(context).repositoriesDao(); - this.accessToken = TokenHelper.getAccessToken((Activity) context); + + if (context instanceof Activity activity) { + this.accessToken = TokenHelper.getAccessToken(activity); + } else if (context instanceof Application app) { + this.accessToken = TokenHelper.getAccessToken(app.getBaseContext()); + } this.username = TokenHelper.getUsername(context); } diff --git a/mobile/src/main/java/io/syslogic/github/dialog/BaseDialogFragment.java b/mobile/src/main/java/io/syslogic/github/dialog/BaseDialogFragment.java index fad1f1f6..72ba8ea6 100644 --- a/mobile/src/main/java/io/syslogic/github/dialog/BaseDialogFragment.java +++ b/mobile/src/main/java/io/syslogic/github/dialog/BaseDialogFragment.java @@ -9,13 +9,11 @@ import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.core.content.ContextCompat; import androidx.databinding.ViewDataBinding; import androidx.fragment.app.DialogFragment; import androidx.preference.PreferenceManager; import io.syslogic.github.BuildConfig; -import io.syslogic.github.R; /** * Base {@link DialogFragment} @@ -61,8 +59,10 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { this.dialog = new Dialog(this.requireContext()); this.dialog.setCanceledOnTouchOutside(this.cancelOnTouchOutSide); if (this.dialog.getWindow() != null) { - this.dialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); - this.dialog.getWindow().setNavigationBarColor(ContextCompat.getColor(requireContext(), R.color.colorPrimaryDark)); + this.dialog.getWindow().setLayout( + ViewGroup.LayoutParams.WRAP_CONTENT, + ViewGroup.LayoutParams.WRAP_CONTENT + ); } return this.dialog; } diff --git a/mobile/src/main/java/io/syslogic/github/fragment/HomeScreenFragment.java b/mobile/src/main/java/io/syslogic/github/fragment/HomeScreenFragment.java index da7cec8b..8da1734a 100644 --- a/mobile/src/main/java/io/syslogic/github/fragment/HomeScreenFragment.java +++ b/mobile/src/main/java/io/syslogic/github/fragment/HomeScreenFragment.java @@ -56,17 +56,17 @@ public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup c } } - /* Navigating to RepositoriesFragment */ + /* Navigate to RepositoriesFragment */ this.mDataBinding.buttonUserRepositories .setOnClickListener(view -> activity.getNavController() - .navigate(R.id.action_homeScreenFragment_to_repositoriesGraph)); + .navigate(R.id.action_homeScreenFragment_to_repositoriesGraph)); - /* Navigating to RepositorySearchFragment */ + /* Navigate to RepositorySearchFragment */ this.mDataBinding.buttonRepositorySearch .setOnClickListener(view -> activity.getNavController() .navigate(R.id.action_homeScreenFragment_to_repositorySearchFragment)); - /* Navigating to GitHub Sponsors */ + /* Navigate to GitHub Sponsors */ this.mDataBinding.textGitHubSponsors .setOnClickListener(view -> activity.startActivity( new Intent(Intent.ACTION_VIEW, Uri.parse(activity.getString(R.string.url_git_hub_sponsors))) diff --git a/mobile/src/main/java/io/syslogic/github/fragment/RepositorySearchFragment.java b/mobile/src/main/java/io/syslogic/github/fragment/RepositorySearchFragment.java index 47a48d22..afcf24b2 100644 --- a/mobile/src/main/java/io/syslogic/github/fragment/RepositorySearchFragment.java +++ b/mobile/src/main/java/io/syslogic/github/fragment/RepositorySearchFragment.java @@ -107,22 +107,31 @@ public void onNothingSelected(AdapterView parent) {} Abstraction.executorService.execute(() -> { try { assert dao != null; + String queryString = null; List items = dao.getItems(); - if (items.size() > 0) { - String queryString = items.get(0).toQueryString(); - requireActivity().runOnUiThread(() -> { - RepositorySearchAdapter adapter = new RepositorySearchAdapter(requireContext(), queryString, showRepositoryTopics, 1); - getDataBinding().recyclerviewRepositorySearch.setAdapter(adapter); - PagerState pagerState = getDataBinding().getPagerState(); - if (pagerState != null) { - pagerState.setQueryString(queryString); - getDataBinding().setPagerState(pagerState); - } - }); + if (! items.isEmpty()) { + queryString = items.get(0).toQueryString(); + } else { - if (mDebug) {Log.e(LOG_TAG, "table `query_strings` has no records.");} this.getDataBinding().toolbarRepositorySearch.spinnerQueryString.setVisibility(View.INVISIBLE); + queryString = Constants.DEFAULT_QUERY_STRING; + if (mDebug) { + Log.w(LOG_TAG, "Table `query_strings` currently has no records."); + Log.w(LOG_TAG, "Using thr default value: \"" + Constants.DEFAULT_QUERY_STRING + "\"."); + } } + + String finalQueryString = queryString; + requireActivity().runOnUiThread(() -> { + RepositorySearchAdapter adapter = new RepositorySearchAdapter(requireContext(), finalQueryString, showRepositoryTopics, 1); + getDataBinding().recyclerviewRepositorySearch.setAdapter(adapter); + PagerState pagerState = getDataBinding().getPagerState(); + if (pagerState != null) { + pagerState.setQueryString(finalQueryString); + getDataBinding().setPagerState(pagerState); + } + }); + } catch (IllegalStateException e) { if (mDebug) {Log.e(LOG_TAG, "" + e.getMessage());} } diff --git a/mobile/src/main/java/io/syslogic/github/network/TokenHelper.java b/mobile/src/main/java/io/syslogic/github/network/TokenHelper.java index 091b0421..9cdd7888 100644 --- a/mobile/src/main/java/io/syslogic/github/network/TokenHelper.java +++ b/mobile/src/main/java/io/syslogic/github/network/TokenHelper.java @@ -47,32 +47,34 @@ public class TokenHelper { static final boolean mDebug = BuildConfig.DEBUG; @Nullable - public static String getAccessToken(@NonNull Activity activity) { - AccountManager accountManager = AccountManager.get(activity); + public static String getAccessToken(@NonNull Context context) { + AccountManager accountManager = AccountManager.get(context); Account account = getAccount(accountManager, 0); if (account != null) { /* Default: Load the access token from AccountManager. */ return accountManager.getUserData(account, "token"); } else if (mDebug) { /* Debug: Try to load and validate the access token. */ - return loadTokenFromPackageMeta(activity, accountManager); + return loadTokenFromPackageMeta(context, accountManager); } else { Log.e(LOG_TAG, "Account not found: " + Constants.ACCOUNT_TYPE); return null; } } - public static void setAccessToken(Activity activity, @Nullable String token) { - AccountManager accountManager = AccountManager.get(activity); + public static void setAccessToken(Context context, @Nullable String token) { + AccountManager accountManager = AccountManager.get(context); Account account = getAccount(accountManager, 0); if (account != null && token == null) { - accountManager.removeAccount(account, activity, accountManagerFuture -> { - Log.d(LOG_TAG, "Account removed: " + Constants.ACCOUNT_TYPE); - }, new Handler(Looper.getMainLooper())); + if (context instanceof Activity activity) { + accountManager.removeAccount(account, activity, accountManagerFuture -> { + Log.d(LOG_TAG, "Account removed: " + Constants.ACCOUNT_TYPE); + }, new Handler(Looper.getMainLooper())); + } } else if (account == null && token == null) { /* This maybe happen when the token loaded from package-meta has expired. */ - Intent intent = new Intent(activity, AuthenticatorActivity.class); - activity.startActivity(intent); + Intent intent = new Intent(context, AuthenticatorActivity.class); + context.startActivity(intent); } } @@ -91,16 +93,16 @@ public static String getUsername(@NonNull Context context) { } @Nullable - private static String loadTokenFromPackageMeta(@NonNull Activity activity, AccountManager accountManager) { + private static String loadTokenFromPackageMeta(@NonNull Context context, AccountManager accountManager) { String token = null; try { ApplicationInfo app; if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.TIRAMISU) { PackageManager.ApplicationInfoFlags flags = PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA); - app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), flags); + app = context.getPackageManager().getApplicationInfo(context.getPackageName(), flags); } else { - app = activity.getPackageManager().getApplicationInfo(activity.getPackageName(), PackageManager.GET_META_DATA); + app = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); } if (mDebug && app.metaData.keySet().contains("com.github.ACCESS_TOKEN")) { @@ -134,9 +136,9 @@ public void onResponse(@NonNull Call call, @NonNull Response respons if (message.equals("\"Bad credentials\"")) { // Remove the token, it is invalid anyway. - TokenHelper.setAccessToken(activity, null); + TokenHelper.setAccessToken(context, null); - if (activity instanceof NavHostActivity activity2) { + if (context instanceof NavHostActivity activity2) { if (activity2.getCurrentFragment() instanceof HomeScreenFragment) { activity2.getNavController().navigate( R.id.action_homeScreenFragment_to_preferencesFragment diff --git a/mobile/src/main/res/drawable-night/ic_baseline_bookmark_add_48.xml b/mobile/src/main/res/drawable-night/ic_baseline_bookmark_add_48.xml new file mode 100644 index 00000000..a507c540 --- /dev/null +++ b/mobile/src/main/res/drawable-night/ic_baseline_bookmark_add_48.xml @@ -0,0 +1,13 @@ + + + + + diff --git a/mobile/src/main/res/drawable-night/ic_baseline_bookmarks_48.xml b/mobile/src/main/res/drawable-night/ic_baseline_bookmarks_48.xml index f87dc7f1..a248a729 100644 --- a/mobile/src/main/res/drawable-night/ic_baseline_bookmarks_48.xml +++ b/mobile/src/main/res/drawable-night/ic_baseline_bookmarks_48.xml @@ -2,11 +2,13 @@ + android:viewportWidth="24" + android:height="48dp" + android:width="48dp"> + + diff --git a/mobile/src/main/res/drawable-night/ic_baseline_cloud_download_24.xml b/mobile/src/main/res/drawable-night/ic_baseline_cloud_download_24.xml index 67d4ac7d..3a886330 100644 --- a/mobile/src/main/res/drawable-night/ic_baseline_cloud_download_24.xml +++ b/mobile/src/main/res/drawable-night/ic_baseline_cloud_download_24.xml @@ -6,7 +6,9 @@ android:viewportWidth="24" android:height="24dp" android:width="24dp"> + + diff --git a/mobile/src/main/res/drawable-night/baseline_folder_copy_24.xml b/mobile/src/main/res/drawable-night/ic_baseline_folder_copy_24.xml similarity index 100% rename from mobile/src/main/res/drawable-night/baseline_folder_copy_24.xml rename to mobile/src/main/res/drawable-night/ic_baseline_folder_copy_24.xml diff --git a/mobile/src/main/res/drawable/ic_baseline_bookmark_add_48.xml b/mobile/src/main/res/drawable/ic_baseline_bookmark_add_48.xml index 61e30bb0..bcd007f5 100644 --- a/mobile/src/main/res/drawable/ic_baseline_bookmark_add_48.xml +++ b/mobile/src/main/res/drawable/ic_baseline_bookmark_add_48.xml @@ -1,5 +1,13 @@ - - + + + + diff --git a/mobile/src/main/res/drawable/ic_baseline_bookmarks_48.xml b/mobile/src/main/res/drawable/ic_baseline_bookmarks_48.xml index 35948f6f..dfffcdad 100644 --- a/mobile/src/main/res/drawable/ic_baseline_bookmarks_48.xml +++ b/mobile/src/main/res/drawable/ic_baseline_bookmarks_48.xml @@ -2,11 +2,13 @@ + android:viewportWidth="24" + android:height="48dp" + android:width="48dp"> + + diff --git a/mobile/src/main/res/drawable/ic_baseline_cloud_download_24.xml b/mobile/src/main/res/drawable/ic_baseline_cloud_download_24.xml index 26e491e1..063f715c 100644 --- a/mobile/src/main/res/drawable/ic_baseline_cloud_download_24.xml +++ b/mobile/src/main/res/drawable/ic_baseline_cloud_download_24.xml @@ -6,7 +6,9 @@ android:viewportWidth="24" android:height="24dp" android:width="24dp"> + + diff --git a/mobile/src/main/res/drawable/baseline_folder_copy_24.xml b/mobile/src/main/res/drawable/ic_baseline_folder_copy_24.xml similarity index 99% rename from mobile/src/main/res/drawable/baseline_folder_copy_24.xml rename to mobile/src/main/res/drawable/ic_baseline_folder_copy_24.xml index e37ecc09..e2ecef85 100644 --- a/mobile/src/main/res/drawable/baseline_folder_copy_24.xml +++ b/mobile/src/main/res/drawable/ic_baseline_folder_copy_24.xml @@ -6,10 +6,13 @@ android:viewportWidth="24" android:height="24dp" android:width="24dp"> + + + diff --git a/mobile/src/main/res/layout/toolbar_download.xml b/mobile/src/main/res/layout/toolbar_download.xml index 62a20b3a..2a3bdb2c 100644 --- a/mobile/src/main/res/layout/toolbar_download.xml +++ b/mobile/src/main/res/layout/toolbar_download.xml @@ -50,7 +50,7 @@ android:layout_width="48dp" android:layout_height="match_parent" android:background="@null" - android:src="@drawable/baseline_folder_copy_24"/> + android:src="@drawable/ic_baseline_folder_copy_24"/> diff --git a/version.properties b/version.properties deleted file mode 100644 index 81ac5417..00000000 --- a/version.properties +++ /dev/null @@ -1,2 +0,0 @@ -versionName = 1.0.3 -versionCode = 3