From da40c3c5d65afdffec0b31887c90da50dc36623c Mon Sep 17 00:00:00 2001 From: inotia00 Date: Mon, 2 Jan 2023 17:59:31 +0900 Subject: [PATCH] cleanup --- .../android/gms/common/data/DataHolder.java | 64 +---------- .../android/gms/common/data/Freezable.java | 39 ------- .../android/gms/common/images/WebImage.java | 6 +- .../microg/gms/AbstractGmsServiceBroker.java | 30 ++---- .../main/java/org/microg/gms/BaseService.java | 3 +- .../kotlin/org/microg/gms/profile/Build.kt | 3 - .../org/microg/gms/profile/ProfileManager.kt | 24 +++-- .../microg/mgms/settings/SettingsContract.kt | 12 +-- .../microg/mgms/settings/SettingsProvider.kt | 12 +-- .../android/gms/common/ConnectionResult.java | 39 ------- .../com/google/android/gms/common/Scopes.java | 22 ---- .../gms/common/api/CommonStatusCodes.java | 54 ---------- .../google/android/gms/common/api/Scope.java | 14 ++- .../google/android/gms/common/api/Status.java | 83 -------------- .../android/gms/dynamic/ObjectWrapper.java | 4 +- .../org/microg/gms/auth/AuthConstants.java | 1 - .../java/org/microg/gms/common/Constants.java | 1 - .../org/microg/gms/common/GmsService.java | 20 +--- .../android/gms/dynamic/ObjectWrapper.kt | 6 +- .../kotlin/org/microg/gms/auth/AuthPrefs.kt | 16 +-- .../microg/gms/auth/appcert/AppCertManager.kt | 18 ++-- .../org/microg/gms/checkin/CheckinPrefs.kt | 28 +++-- .../org/microg/gms/checkin/LastCheckinInfo.kt | 60 ++++++----- .../org/microg/gms/checkin/ServiceInfo.kt | 20 ++-- .../kotlin/org/microg/gms/gcm/GcmPrefs.kt | 101 +++++++++++------- .../org/microg/gms/gcm/PushRegisterService.kt | 46 ++++---- .../kotlin/org/microg/gms/gcm/ServiceInfo.kt | 8 +- .../ui/PushNotificationPreferencesFragment.kt | 4 +- .../org/microg/gms/ui/SettingsFragment.kt | 2 +- 29 files changed, 238 insertions(+), 502 deletions(-) delete mode 100644 play-services-base-api/src/main/java/com/google/android/gms/common/data/Freezable.java delete mode 100644 play-services-basement/src/main/java/com/google/android/gms/common/Scopes.java diff --git a/play-services-base-api/src/main/java/com/google/android/gms/common/data/DataHolder.java b/play-services-base-api/src/main/java/com/google/android/gms/common/data/DataHolder.java index f7b8e46a..77acbb1b 100644 --- a/play-services-base-api/src/main/java/com/google/android/gms/common/data/DataHolder.java +++ b/play-services-base-api/src/main/java/com/google/android/gms/common/data/DataHolder.java @@ -17,12 +17,13 @@ package com.google.android.gms.common.data; import android.annotation.SuppressLint; -import android.content.ContentValues; import android.database.Cursor; import android.database.CursorWindow; import android.os.Bundle; import android.os.Parcel; +import androidx.annotation.NonNull; + import org.microg.gms.common.PublicApi; import org.microg.safeparcel.AutoSafeParcelable; import org.microg.safeparcel.SafeParceled; @@ -93,24 +94,12 @@ public class DataHolder extends AutoSafeParcelable implements Closeable { * Get a {@link DataHolder.Builder} to create a new {@link DataHolder} manually. * * @param columns The array of column names that the object supports. - * @param uniqueColumn The non-null column name that must contain unique values. New rows added to the builder with the same value in this column will replace any older rows. - * @return {@link DataHolder.Builder} object to work with. - */ - public static Builder builder(String[] columns, String uniqueColumn) { - return new Builder(columns, uniqueColumn); - } - - /** - * Get a {@link DataHolder.Builder} to create a new {@link DataHolder} manually. - * - * @param columns The array of column names that the object supports. * @return {@link DataHolder.Builder} object to work with. */ public static Builder builder(String[] columns) { - return builder(columns, null); + return new Builder(columns); } - @SuppressWarnings("deprecation") @SuppressLint({"NewApi", "ObsoleteSdkInt"}) static int getCursorType(Cursor cursor, int i) { return cursor.getType(i); @@ -252,6 +241,7 @@ public class DataHolder extends AutoSafeParcelable implements Closeable { return windows[windowIndex].getString(row, columnIndices.get(column)); } + @NonNull @Override public String toString() { return "DataHolder{" + @@ -277,14 +267,10 @@ public class DataHolder extends AutoSafeParcelable implements Closeable { public static class Builder { private final String[] columns; private final ArrayList> rows; - private final String uniqueColumn; - private final Map uniqueIndizes; - private Builder(String[] columns, String uniqueColumn) { + private Builder(String[] columns) { this.columns = columns; this.rows = new ArrayList<>(); - this.uniqueColumn = uniqueColumn; - this.uniqueIndizes = new HashMap<>(); } /** @@ -315,46 +301,6 @@ public class DataHolder extends AutoSafeParcelable implements Closeable { return rows.size(); } - /** - * Add a new row of data to the {@link DataHolder} this {@link DataHolder.Builder} will create. Note that the data must contain an entry for all columns - *

- * Currently the only supported value types that are supported are String, Long, and Boolean (Integer is also accepted and will be stored as a Long). - * - * @param values {@link ContentValues} containing row data. - * @return {@link DataHolder.Builder} to continue construction. - */ - public Builder withRow(ContentValues values) { - HashMap row = new HashMap<>(); - for (Map.Entry entry : values.valueSet()) { - row.put(entry.getKey(), entry.getValue()); - } - return withRow(row); - } - - /** - * Add a new row of data to the {@link DataHolder} this {@link DataHolder.Builder} will create. Note that the data must contain an entry for all columns - *

- * Currently the only supported value types that are supported are String, Long, and Boolean (Integer is also accepted and will be stored as a Long). - * - * @param row Map containing row data. - * @return {@link DataHolder.Builder} to continue construction. - */ - public Builder withRow(HashMap row) { - if (uniqueColumn != null) { - Object val = row.get(uniqueColumn); - if (val != null) { - Integer old = uniqueIndizes.get(val); - if (old != null) { - rows.set(old, row); - return this; - } else { - uniqueIndizes.put(val, rows.size()); - } - } - } - rows.add(row); - return this; - } } public static final Creator CREATOR = new AutoCreator(DataHolder.class) { diff --git a/play-services-base-api/src/main/java/com/google/android/gms/common/data/Freezable.java b/play-services-base-api/src/main/java/com/google/android/gms/common/data/Freezable.java deleted file mode 100644 index 51f4847e..00000000 --- a/play-services-base-api/src/main/java/com/google/android/gms/common/data/Freezable.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright (C) 2013-2017 microG Project Team - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.gms.common.data; - -public interface Freezable { - /** - * Freeze a volatile representation into an immutable representation. Objects returned from - * this call are safe to cache. - *

- * Note that the output of {@link #freeze} may not be identical to the parent object, but - * should be equal. - * - * @return A concrete implementation of the data object. - */ - T freeze(); - - /** - * Check to see if this object is valid for use. If the object is still volatile, this method - * will indicate whether or not the object can be safely used. - * The output of a call to {@link #freeze()} will always be valid. - * - * @return whether or not the object is valid for use. - */ - boolean isDataValid(); -} diff --git a/play-services-base-api/src/main/java/com/google/android/gms/common/images/WebImage.java b/play-services-base-api/src/main/java/com/google/android/gms/common/images/WebImage.java index fee75495..4b5e4ad7 100644 --- a/play-services-base-api/src/main/java/com/google/android/gms/common/images/WebImage.java +++ b/play-services-base-api/src/main/java/com/google/android/gms/common/images/WebImage.java @@ -18,6 +18,8 @@ package com.google.android.gms.common.images; import android.net.Uri; +import androidx.annotation.NonNull; + import org.microg.safeparcel.AutoSafeParcelable; import org.microg.safeparcel.SafeParceled; @@ -58,7 +60,9 @@ public class WebImage extends AutoSafeParcelable { return height; } + @NonNull public String toString() { - return String.format(Locale.getDefault(), "Image %dx%d %s", width, height, uri.toString()); + assert uri != null; + return String.format(Locale.getDefault(), "Image %dx%d %s", width, height, uri); } } diff --git a/play-services-base-core/src/main/java/org/microg/gms/AbstractGmsServiceBroker.java b/play-services-base-core/src/main/java/org/microg/gms/AbstractGmsServiceBroker.java index 1cd39310..9ff67ff8 100644 --- a/play-services-base-core/src/main/java/org/microg/gms/AbstractGmsServiceBroker.java +++ b/play-services-base-core/src/main/java/org/microg/gms/AbstractGmsServiceBroker.java @@ -16,14 +16,11 @@ package org.microg.gms; -import android.accounts.Account; import android.os.Bundle; -import android.os.IBinder; import android.os.Parcel; import android.os.RemoteException; import android.util.Log; -import com.google.android.gms.common.api.Scope; import com.google.android.gms.common.internal.GetServiceRequest; import com.google.android.gms.common.internal.IGmsCallbacks; import com.google.android.gms.common.internal.IGmsServiceBroker; @@ -59,37 +56,24 @@ public abstract class AbstractGmsServiceBroker extends IGmsServiceBroker.Stub { @Override public void getAddressService(IGmsCallbacks callback, int versionCode, String packageName) throws RemoteException { - callGetService(GmsService.ADDRESS, callback, versionCode, packageName); + callGetService(callback, versionCode, packageName); } - private void callGetService(GmsService service, IGmsCallbacks callback, int gmsVersion, + private void callGetService(IGmsCallbacks callback, int gmsVersion, String packageName) throws RemoteException { - callGetService(service, callback, gmsVersion, packageName, null); + callGetService(GmsService.ADDRESS, callback, gmsVersion, packageName, null); } - private void callGetService(GmsService service, IGmsCallbacks callback, int gmsVersion, - String packageName, Bundle extras) throws RemoteException { - callGetService(service, callback, gmsVersion, packageName, extras, null, null); - } - - private void callGetService(GmsService service, IGmsCallbacks callback, int gmsVersion, String packageName, Bundle extras, String accountName, String[] scopes) throws RemoteException { + private void callGetService(GmsService service, IGmsCallbacks callback, int gmsVersion, String packageName, Bundle extras) throws RemoteException { GetServiceRequest request = new GetServiceRequest(service.SERVICE_ID); request.gmsVersion = gmsVersion; request.packageName = packageName; request.extras = extras; - request.account = accountName == null ? null : new Account(accountName, "com.mgoogle"); - request.scopes = scopes == null ? null : scopesFromStringArray(scopes); + request.account = null; + request.scopes = null; getService(callback, request); } - private Scope[] scopesFromStringArray(String[] arr) { - Scope[] scopes = new Scope[arr.length]; - for (int i = 0; i < arr.length; i++) { - scopes[i] = new Scope(arr[i]); - } - return scopes; - } - @Override public void getService(IGmsCallbacks callback, GetServiceRequest request) throws RemoteException { GmsService gmsService = GmsService.byServiceId(request.serviceId); @@ -104,7 +88,7 @@ public abstract class AbstractGmsServiceBroker extends IGmsServiceBroker.Stub { public abstract void handleServiceRequest(IGmsCallbacks callback, GetServiceRequest request, GmsService service) throws RemoteException; @Override - public void validateAccount(IGmsCallbacks callback, ValidateAccountRequest request) throws RemoteException { + public void validateAccount(IGmsCallbacks callback, ValidateAccountRequest request) { throw new IllegalArgumentException("ValidateAccountRequest not supported"); } diff --git a/play-services-base-core/src/main/java/org/microg/gms/BaseService.java b/play-services-base-core/src/main/java/org/microg/gms/BaseService.java index 3941e508..38545ff0 100644 --- a/play-services-base-core/src/main/java/org/microg/gms/BaseService.java +++ b/play-services-base-core/src/main/java/org/microg/gms/BaseService.java @@ -21,6 +21,7 @@ import android.os.IBinder; import android.os.RemoteException; import android.util.Log; +import androidx.annotation.NonNull; import androidx.lifecycle.LifecycleService; import com.google.android.gms.common.internal.GetServiceRequest; @@ -58,7 +59,7 @@ public abstract class BaseService extends LifecycleService { } @Override - public IBinder onBind(Intent intent) { + public IBinder onBind(@NonNull Intent intent) { super.onBind(intent); Log.d(TAG, "onBind: " + intent); return broker.asBinder(); diff --git a/play-services-base-core/src/main/kotlin/org/microg/gms/profile/Build.kt b/play-services-base-core/src/main/kotlin/org/microg/gms/profile/Build.kt index a52059a9..c0398c2b 100644 --- a/play-services-base-core/src/main/kotlin/org/microg/gms/profile/Build.kt +++ b/play-services-base-core/src/main/kotlin/org/microg/gms/profile/Build.kt @@ -5,8 +5,6 @@ package org.microg.gms.profile -import android.annotation.TargetApi - object Build { @JvmField var BOARD: String? = null @@ -24,7 +22,6 @@ object Build { var CPU_ABI2: String? = null @JvmField - @TargetApi(21) var SUPPORTED_ABIS: Array = emptyArray() @JvmField diff --git a/play-services-base-core/src/main/kotlin/org/microg/gms/profile/ProfileManager.kt b/play-services-base-core/src/main/kotlin/org/microg/gms/profile/ProfileManager.kt index ed756017..0cbc69b8 100644 --- a/play-services-base-core/src/main/kotlin/org/microg/gms/profile/ProfileManager.kt +++ b/play-services-base-core/src/main/kotlin/org/microg/gms/profile/ProfileManager.kt @@ -32,7 +32,11 @@ object ProfileManager { private fun getSystemProfileFile(): File = File("/system/etc/microg_device_profile.xml") private fun getProfileResId(context: Context, profile: String) = context.resources.getIdentifier("${context.packageName}:xml/profile_$profile".lowercase(Locale.US), null, null) - fun getConfiguredProfile(context: Context): String = SettingsContract.getSettings(context, Profile.getContentUri(context), arrayOf(Profile.PROFILE)) { it.getString(0) } ?: PROFILE_AUTO + fun getConfiguredProfile(context: Context): String = Profile.getContentUri(context)?.let { + SettingsContract.getSettings(context, + it, arrayOf(Profile.PROFILE)) { it.getString(0) } + } + ?: PROFILE_AUTO fun getAutoProfile(context: Context): String { if (hasProfile(context, PROFILE_SYSTEM) && isAutoProfile(context, PROFILE_SYSTEM)) return PROFILE_SYSTEM @@ -115,8 +119,14 @@ object ProfileManager { } private fun getProfile(context: Context) = getConfiguredProfile(context).let { if (it != PROFILE_AUTO) it else getAutoProfile(context) } - private fun getSerialFromSettings(context: Context): String? = SettingsContract.getSettings(context, Profile.getContentUri(context), arrayOf(Profile.SERIAL)) { it.getString(0) } - private fun saveSerial(context: Context, serial: String) = SettingsContract.setSettings(context, Profile.getContentUri(context)) { put(Profile.SERIAL, serial) } + private fun getSerialFromSettings(context: Context): String? = + Profile.getContentUri(context)?.let { + SettingsContract.getSettings(context, + it, arrayOf(Profile.SERIAL)) { it.getString(0) } + } + private fun saveSerial(context: Context, serial: String) = + Profile.getContentUri(context) + ?.let { SettingsContract.setSettings(context, it) { put(Profile.SERIAL, serial) } } private fun randomSerial(template: String, prefixLength: Int = (template.length / 2).coerceAtMost(6)): String { val serial = StringBuilder() @@ -294,9 +304,11 @@ object ProfileManager { val changed = getProfile(context) != profile val newProfile = profile ?: PROFILE_AUTO val newSerial = if (changed) getSerial(context, newProfile, true) else getSerial(context) - SettingsContract.setSettings(context, Profile.getContentUri(context)) { - put(Profile.PROFILE, newProfile) - if (changed) put(Profile.SERIAL, newSerial) + Profile.getContentUri(context)?.let { + SettingsContract.setSettings(context, it) { + put(Profile.PROFILE, newProfile) + if (changed) put(Profile.SERIAL, newSerial) + } } if (changed && activeProfile != null) applyProfile(context, newProfile, newSerial) } diff --git a/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsContract.kt b/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsContract.kt index 841b47a0..4f930962 100644 --- a/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsContract.kt +++ b/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsContract.kt @@ -12,8 +12,7 @@ object SettingsContract { object CheckIn { private const val id = "check-in" - fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id) - fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id" + fun getContentUri(context: Context): Uri? = Uri.withAppendedPath(getAuthorityUri(context), id) const val ENABLED = "checkin_enable_service" const val ANDROID_ID = "androidId" @@ -44,8 +43,7 @@ object SettingsContract { object Gcm { private const val id = "gcm" - fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id) - fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id" + fun getContentUri(context: Context): Uri? = Uri.withAppendedPath(getAuthorityUri(context), id) const val FULL_LOG = "gcm_full_log" const val LAST_PERSISTENT_ID = "gcm_last_persistent_id" @@ -76,8 +74,7 @@ object SettingsContract { object Auth { private const val id = "auth" - fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id) - fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id" + fun getContentUri(context: Context): Uri? = Uri.withAppendedPath(getAuthorityUri(context), id) const val TRUST_GOOGLE = "auth_manager_trust_google" const val VISIBLE = "auth_manager_visible" @@ -90,8 +87,7 @@ object SettingsContract { object Profile { private const val id = "profile" - fun getContentUri(context: Context) = Uri.withAppendedPath(getAuthorityUri(context), id) - fun getContentType(context: Context) = "vnd.android.cursor.item/vnd.${getAuthority(context)}.$id" + fun getContentUri(context: Context): Uri? = Uri.withAppendedPath(getAuthorityUri(context), id) const val PROFILE = "device_profile" const val SERIAL = "device_profile_serial" diff --git a/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsProvider.kt b/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsProvider.kt index f1829b2c..ba7e2e25 100644 --- a/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsProvider.kt +++ b/play-services-base-core/src/main/kotlin/org/microg/mgms/settings/SettingsProvider.kt @@ -6,6 +6,7 @@ package org.microg.mgms.settings +import android.annotation.SuppressLint import android.content.ContentProvider import android.content.ContentValues import android.content.Context @@ -27,6 +28,7 @@ import java.io.File * All settings access should go through this [ContentProvider], * because it provides safe access from different processes which normal [SharedPreferences] don't. */ +@SuppressLint("DiscouragedPrivateApi") class SettingsProvider : ContentProvider() { companion object { const val INTERVAL = 1 * 60 * 1000 // 1 minute @@ -165,9 +167,9 @@ class SettingsProvider : ContentProvider() { Gcm.NETWORK_ROAMING -> Integer.parseInt(preferences.getString(key, "0") ?: "0") Gcm.NETWORK_OTHER -> Integer.parseInt(preferences.getString(key, "0") ?: "0") - Gcm.LEARNT_MOBILE -> preferences.getInt(key, Companion.INTERVAL) - Gcm.LEARNT_WIFI -> preferences.getInt(key, Companion.INTERVAL) - Gcm.LEARNT_OTHER -> preferences.getInt(key, Companion.INTERVAL) + Gcm.LEARNT_MOBILE -> preferences.getInt(key, INTERVAL) + Gcm.LEARNT_WIFI -> preferences.getInt(key, INTERVAL) + Gcm.LEARNT_OTHER -> preferences.getInt(key, INTERVAL) else -> throw IllegalArgumentException("Unknown key: $key") } @@ -271,12 +273,8 @@ class SettingsProvider : ContentProvider() { } private fun getSettingsString(key: String, def: String? = null): String? = listOf(preferences, systemDefaultPreferences).getString(key, def) - private fun getSettingsInt(key: String, def: Int): Int = listOf(preferences, systemDefaultPreferences).getInt(key, def) - private fun getSettingsLong(key: String, def: Long): Long = listOf(preferences, systemDefaultPreferences).getLong(key, def) private fun List.getString(key: String, def: String?): String? = foldRight(def) { preferences, defValue -> preferences?.getString(key, defValue) ?: defValue } - private fun List.getInt(key: String, def: Int): Int = foldRight(def) { preferences, defValue -> preferences?.getInt(key, defValue) ?: defValue } - private fun List.getLong(key: String, def: Long): Long = foldRight(def) { preferences, defValue -> preferences?.getLong(key, defValue) ?: defValue } private fun List.getBoolean(key: String, def: Boolean): Boolean = foldRight(def) { preferences, defValue -> preferences?.getBoolean(key, defValue) ?: defValue } private fun List.getBooleanAsInt(key: String, def: Boolean): Int = if (getBoolean(key, def)) 1 else 0 } diff --git a/play-services-basement/src/main/java/com/google/android/gms/common/ConnectionResult.java b/play-services-basement/src/main/java/com/google/android/gms/common/ConnectionResult.java index f3272de8..23ebfb81 100644 --- a/play-services-basement/src/main/java/com/google/android/gms/common/ConnectionResult.java +++ b/play-services-basement/src/main/java/com/google/android/gms/common/ConnectionResult.java @@ -18,7 +18,6 @@ package com.google.android.gms.common; import android.app.Activity; import android.app.PendingIntent; -import android.content.Intent; import android.content.IntentSender; import android.text.TextUtils; @@ -239,35 +238,6 @@ public class ConnectionResult { } } - /** - * Indicates the type of error that interrupted connection. - * - * @return the error code, or {@link #SUCCESS} if no error occurred. - */ - public int getErrorCode() { - return statusCode; - } - - /** - * Returns an error message for connection result. - * - * @return the message - */ - public String getErrorMessage() { - return message; - } - - /** - * A pending intent to resolve the connection failure. This intent can be started with - * {@link Activity#startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} to - * present UI to solve the issue. - * - * @return The pending intent to resolve the connection failure. - */ - public PendingIntent getResolution() { - return pendingIntent; - } - @Override public int hashCode() { return Arrays.hashCode(new Object[]{statusCode, pendingIntent, message}); @@ -283,15 +253,6 @@ public class ConnectionResult { return statusCode != 0 && pendingIntent != null; } - /** - * Returns {@code true} if the connection was successful. - * - * @return {@code true} if the connection was successful, {@code false} if there was an error. - */ - public boolean isSuccess() { - return statusCode == 0; - } - /** * Resolves an error by starting any intents requiring user interaction. See * {@link #SIGN_IN_REQUIRED}, and {@link #RESOLUTION_REQUIRED}. diff --git a/play-services-basement/src/main/java/com/google/android/gms/common/Scopes.java b/play-services-basement/src/main/java/com/google/android/gms/common/Scopes.java deleted file mode 100644 index 520bef02..00000000 --- a/play-services-basement/src/main/java/com/google/android/gms/common/Scopes.java +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (C) 2013-2017 microG Project Team - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.android.gms.common; - -public class Scopes { - public static final String PROFILE = "profile"; - public static final String APP_STATE = "https://www.googleapis.com/auth/appstate"; -} diff --git a/play-services-basement/src/main/java/com/google/android/gms/common/api/CommonStatusCodes.java b/play-services-basement/src/main/java/com/google/android/gms/common/api/CommonStatusCodes.java index 6a2cbb51..984c6fce 100644 --- a/play-services-basement/src/main/java/com/google/android/gms/common/api/CommonStatusCodes.java +++ b/play-services-basement/src/main/java/com/google/android/gms/common/api/CommonStatusCodes.java @@ -16,13 +16,10 @@ package com.google.android.gms.common.api; -import androidx.annotation.NonNull; - import org.microg.gms.common.PublicApi; @PublicApi public class CommonStatusCodes { - public static final int SUCCESS_CACHE = -1; public static final int SUCCESS = 0; @Deprecated public static final int SERVICE_MISSING = 1; @@ -31,61 +28,10 @@ public class CommonStatusCodes { @Deprecated public static final int SERVICE_DISABLED = 3; public static final int SIGN_IN_REQUIRED = 4; - public static final int INVALID_ACCOUNT = 5; public static final int RESOLUTION_REQUIRED = 6; - public static final int NETWORK_ERROR = 7; public static final int INTERNAL_ERROR = 8; - public static final int SERVICE_INVALID = 9; - public static final int DEVELOPER_ERROR = 10; - public static final int LICENSE_CHECK_FAILED = 11; public static final int ERROR = 13; public static final int INTERRUPTED = 14; - public static final int TIMEOUT = 15; public static final int CANCELED = 16; - public static final int API_NOT_CONNECTED = 17; - public static final int DEAD_CLIENT = 18; - @NonNull - public static String getStatusCodeString(int statusCode) { - switch (statusCode) { - case SUCCESS_CACHE: - return "SUCCESS_CACHE"; - case SUCCESS: - return "SUCCESS"; - case SERVICE_VERSION_UPDATE_REQUIRED: - return "SERVICE_VERSION_UPDATE_REQUIRED"; - case SERVICE_DISABLED: - return "SERVICE_DISABLED"; - case SIGN_IN_REQUIRED: - return "SIGN_IN_REQUIRED"; - case INVALID_ACCOUNT: - return "INVALID_ACCOUNT"; - case RESOLUTION_REQUIRED: - return "RESOLUTION_REQUIRED"; - case NETWORK_ERROR: - return "NETWORK_ERROR"; - case INTERNAL_ERROR: - return "INTERNAL_ERROR"; - case SERVICE_INVALID: - return "SERVICE_INVALID"; - case DEVELOPER_ERROR: - return "DEVELOPER_ERROR"; - case LICENSE_CHECK_FAILED: - return "LICENSE_CHECK_FAILED"; - case ERROR: - return "ERROR"; - case INTERRUPTED: - return "INTERRUPTED"; - case TIMEOUT: - return "TIMEOUT"; - case CANCELED: - return "CANCELED"; - case API_NOT_CONNECTED: - return "API_NOT_CONNECTED"; - case DEAD_CLIENT: - return "DEAD_CLIENT"; - default: - return "unknown status code: " + statusCode; - } - } } diff --git a/play-services-basement/src/main/java/com/google/android/gms/common/api/Scope.java b/play-services-basement/src/main/java/com/google/android/gms/common/api/Scope.java index 60bc1fb9..a90803e7 100644 --- a/play-services-basement/src/main/java/com/google/android/gms/common/api/Scope.java +++ b/play-services-basement/src/main/java/com/google/android/gms/common/api/Scope.java @@ -16,6 +16,8 @@ package com.google.android.gms.common.api; +import androidx.annotation.NonNull; + import org.microg.gms.common.PublicApi; import org.microg.safeparcel.AutoSafeParcelable; import org.microg.safeparcel.SafeParceled; @@ -44,20 +46,22 @@ public class Scope extends AutoSafeParcelable { @Override public boolean equals(Object o) { - return this == o || o instanceof Scope && scopeUri.equals(((Scope) o).scopeUri); - } - - public String getScopeUri() { - return scopeUri; + if (this == o) return true; + if (!(o instanceof Scope)) return false; + assert scopeUri != null; + return scopeUri.equals(((Scope) o).scopeUri); } @Override public int hashCode() { + assert scopeUri != null; return scopeUri.hashCode(); } + @NonNull @Override public String toString() { + assert scopeUri != null; return scopeUri; } diff --git a/play-services-basement/src/main/java/com/google/android/gms/common/api/Status.java b/play-services-basement/src/main/java/com/google/android/gms/common/api/Status.java index 7b936e67..433ef043 100644 --- a/play-services-basement/src/main/java/com/google/android/gms/common/api/Status.java +++ b/play-services-basement/src/main/java/com/google/android/gms/common/api/Status.java @@ -16,11 +16,7 @@ package com.google.android.gms.common.api; -import android.app.Activity; import android.app.PendingIntent; -import android.content.Intent; -import android.content.IntentSender; -import android.content.IntentSender.SendIntentException; import org.microg.gms.common.PublicApi; import org.microg.safeparcel.AutoSafeParcelable; @@ -44,16 +40,8 @@ public final class Status extends AutoSafeParcelable implements Result { @SafeParceled(1) private final int statusCode; - @SafeParceled(2) - private final String statusMessage; - - @SafeParceled(3) - private final PendingIntent resolution; - private Status() { statusCode = 0; - statusMessage = null; - resolution = null; } /** @@ -84,19 +72,6 @@ public final class Status extends AutoSafeParcelable implements Result { */ public Status(int statusCode, String statusMessage, PendingIntent resolution) { this.statusCode = statusCode; - this.statusMessage = statusMessage; - this.resolution = resolution; - } - - /** - * A pending intent to resolve the failure. This intent can be started with - * {@link Activity#startIntentSenderForResult(IntentSender, int, Intent, int, int, int)} to - * present UI to solve the issue. - * - * @return The pending intent to resolve the failure. - */ - public PendingIntent getResolution() { - return resolution; } /** @@ -111,30 +86,6 @@ public final class Status extends AutoSafeParcelable implements Result { return this; } - /** - * Indicates the status of the operation. - * - * @return Status code resulting from the operation. The value is one of the constants in - * {@link CommonStatusCodes} or specific to the APIs added to the GoogleApiClient. - */ - public int getStatusCode() { - return statusCode; - } - - public String getStatusMessage() { - return statusMessage; - } - - /** - * Returns true if calling {@link #startResolutionForResult(Activity, int)} will start any - * intents requiring user interaction. - * - * @return true if there is a resolution that can be started. - */ - public boolean hasResolution() { - return resolution != null; - } - /** * Returns true if the operation was canceled. */ @@ -142,39 +93,5 @@ public final class Status extends AutoSafeParcelable implements Result { return statusCode == CommonStatusCodes.CANCELED; } - /** - * Returns true if the operation was interrupted. - */ - public boolean isInterrupted() { - return statusCode == CommonStatusCodes.INTERRUPTED; - } - - /** - * Returns true if the operation was successful. - * - * @return true if the operation was successful, false if there was an error. - */ - public boolean isSuccess() { - return statusCode <= 0; - } - - /** - * Resolves an error by starting any intents requiring user interaction. See - * {@link CommonStatusCodes#SIGN_IN_REQUIRED}, and {@link CommonStatusCodes#RESOLUTION_REQUIRED}. - * - * @param activity An Activity context to use to resolve the issue. The activity's - * onActivityResult method will be invoked after the user is done. If the - * resultCode is {@link Activity#RESULT_OK}, the application should try to - * connect again. - * @param requestCode The request code to pass to onActivityResult. - * @throws SendIntentException If the resolution intent has been canceled or is no longer able - * to execute the request. - */ - public void startResolutionForResult(Activity activity, int requestCode) throws SendIntentException { - if (hasResolution()) { - activity.startIntentSenderForResult(resolution.getIntentSender(), requestCode, null, 0, 0, 0); - } - } - public static final Creator CREATOR = new AutoCreator<>(Status.class); } diff --git a/play-services-basement/src/main/java/com/google/android/gms/dynamic/ObjectWrapper.java b/play-services-basement/src/main/java/com/google/android/gms/dynamic/ObjectWrapper.java index 649af71b..9158c941 100644 --- a/play-services-basement/src/main/java/com/google/android/gms/dynamic/ObjectWrapper.java +++ b/play-services-basement/src/main/java/com/google/android/gms/dynamic/ObjectWrapper.java @@ -35,7 +35,7 @@ public class ObjectWrapper extends IObjectWrapper.Stub { return null; } if (obj instanceof ObjectWrapper) { - return ((ObjectWrapper) obj).t; + return ((ObjectWrapper) obj).t; } IBinder binder = obj.asBinder(); Field[] fields = binder.getClass().getDeclaredFields(); @@ -95,6 +95,6 @@ public class ObjectWrapper extends IObjectWrapper.Stub { } public static ObjectWrapper wrap(T t) { - return new ObjectWrapper(t); + return new ObjectWrapper<>(t); } } \ No newline at end of file diff --git a/play-services-basement/src/main/java/org/microg/gms/auth/AuthConstants.java b/play-services-basement/src/main/java/org/microg/gms/auth/AuthConstants.java index 52c1dc72..774f5756 100644 --- a/play-services-basement/src/main/java/org/microg/gms/auth/AuthConstants.java +++ b/play-services-basement/src/main/java/org/microg/gms/auth/AuthConstants.java @@ -17,7 +17,6 @@ package org.microg.gms.auth; public class AuthConstants { - public static final String DEFAULT_ACCOUNT = "<>"; public static final String SCOPE_GET_ACCOUNT_ID = "^^_account_id_^^"; public static final String PROVIDER_METHOD_GET_ACCOUNTS = "get_accounts"; public static final String PROVIDER_METHOD_CLEAR_PASSWORD = "clear_password"; diff --git a/play-services-basement/src/main/java/org/microg/gms/common/Constants.java b/play-services-basement/src/main/java/org/microg/gms/common/Constants.java index 217ea437..077bdbde 100644 --- a/play-services-basement/src/main/java/org/microg/gms/common/Constants.java +++ b/play-services-basement/src/main/java/org/microg/gms/common/Constants.java @@ -21,7 +21,6 @@ import org.microg.gms.basement.BuildConfig; public class Constants { public static final int GMS_VERSION_CODE = (BuildConfig.VERSION_CODE / 1000) * 1000; public static final String GMS_PACKAGE_NAME = "com.google.android.gms"; - public static final String GSF_PACKAGE_NAME = "com.mgoogle.android.gsf"; public static final String GMS_PACKAGE_SIGNATURE_SHA1 = "38918a453d07199354f8b19af05ec6562ced5788"; public static final String GMS_SECONDARY_PACKAGE_SIGNATURE_SHA1 = "bd32424203e0fb25f36b57e5aa356f9bdd1da998"; diff --git a/play-services-basement/src/main/java/org/microg/gms/common/GmsService.java b/play-services-basement/src/main/java/org/microg/gms/common/GmsService.java index c9b11ad1..739bc259 100644 --- a/play-services-basement/src/main/java/org/microg/gms/common/GmsService.java +++ b/play-services-basement/src/main/java/org/microg/gms/common/GmsService.java @@ -30,9 +30,9 @@ public enum GmsService { IDENTITY_SIGN_IN(212, "com.google.android.gms.auth.api.identity.service.signin.START"), ; - public int SERVICE_ID; - public String ACTION; - public String[] SECONDARY_ACTIONS; + public final int SERVICE_ID; + public final String ACTION; + public final String[] SECONDARY_ACTIONS; GmsService(int serviceId, String... actions) { this.SERVICE_ID = serviceId; @@ -40,11 +40,6 @@ public enum GmsService { this.SECONDARY_ACTIONS = actions; } - public interface ADVERTISING_ID { - // Has no service id - String ACTION = "com.google.android.gms.ads.identifier.service.START"; - } - public static GmsService byServiceId(int serviceId) { for (GmsService service : values()) { if (service.SERVICE_ID == serviceId) return service; @@ -52,15 +47,6 @@ public enum GmsService { return UNKNOWN; } - public static GmsService byAction(String action) { - for (GmsService service : values()) { - for (String serviceAction : service.SECONDARY_ACTIONS) { - if (serviceAction.equals(action)) return service; - } - } - return UNKNOWN; - } - public static String nameFromServiceId(int serviceId) { return byServiceId(serviceId).toString(serviceId); } diff --git a/play-services-basement/src/main/kotlin/com/google/android/gms/dynamic/ObjectWrapper.kt b/play-services-basement/src/main/kotlin/com/google/android/gms/dynamic/ObjectWrapper.kt index 06c131ab..27996b8f 100644 --- a/play-services-basement/src/main/kotlin/com/google/android/gms/dynamic/ObjectWrapper.kt +++ b/play-services-basement/src/main/kotlin/com/google/android/gms/dynamic/ObjectWrapper.kt @@ -14,9 +14,5 @@ * limitations under the License. */ -package org.microg.gms.kotlin +package com.google.android.gms.dynamic -import com.google.android.gms.dynamic.IObjectWrapper -import com.google.android.gms.dynamic.ObjectWrapper - -inline fun IObjectWrapper?.unwrap(): T? = ObjectWrapper.unwrapTyped(this, T::class.java) diff --git a/play-services-core/src/main/kotlin/org/microg/gms/auth/AuthPrefs.kt b/play-services-core/src/main/kotlin/org/microg/gms/auth/AuthPrefs.kt index 5406bf2a..a86db122 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/auth/AuthPrefs.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/auth/AuthPrefs.kt @@ -7,16 +7,20 @@ import org.microg.mgms.settings.SettingsContract.Auth object AuthPrefs { @JvmStatic - fun isTrustGooglePermitted(context: Context): Boolean { - return SettingsContract.getSettings(context, Auth.getContentUri(context), arrayOf(Auth.TRUST_GOOGLE)) { c -> - c.getInt(0) != 0 + fun isTrustGooglePermitted(context: Context): Boolean? { + return Auth.getContentUri(context)?.let { + SettingsContract.getSettings(context, it, arrayOf(Auth.TRUST_GOOGLE)) { c -> + c.getInt(0) != 0 + } } } @JvmStatic - fun isAuthVisible(context: Context): Boolean { - return SettingsContract.getSettings(context, Auth.getContentUri(context), arrayOf(Auth.VISIBLE)) { c -> - c.getInt(0) != 0 + fun isAuthVisible(context: Context): Boolean? { + return Auth.getContentUri(context)?.let { + SettingsContract.getSettings(context, it, arrayOf(Auth.VISIBLE)) { c -> + c.getInt(0) != 0 + } } } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/auth/appcert/AppCertManager.kt b/play-services-core/src/main/kotlin/org/microg/gms/auth/appcert/AppCertManager.kt index 8e5d5036..3120a555 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/auth/appcert/AppCertManager.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/auth/appcert/AppCertManager.kt @@ -7,7 +7,6 @@ package org.microg.gms.auth.appcert import android.content.Context import android.database.Cursor -import android.os.SystemClock import android.util.Base64 import android.util.Log import com.android.volley.NetworkResponse @@ -63,7 +62,7 @@ class AppCertManager(private val context: Context) { Log.w(TAG, "DeviceKeys for app certifications are experimental") deviceKeyCacheTime = currentTime val lastCheckinInfo = LastCheckinInfo.read(context) - val androidId = lastCheckinInfo.androidId + val androidId = lastCheckinInfo?.androidId val sessionId = Random.nextLong() val token = completeRegisterRequest(context, GcmDatabase(context), RegisterRequest().build(context) .checkin(lastCheckinInfo) @@ -76,7 +75,7 @@ class AppCertManager(private val context: Context) { .extraParam("scope", REGISTER_SCOPE)) .getString(GcmConstants.EXTRA_REGISTRATION_ID) val request = DeviceKeyRequest( - androidId = lastCheckinInfo.androidId, + androidId = lastCheckinInfo?.androidId, sessionId = sessionId, versionInfo = DeviceKeyRequest.VersionInfo(Build.VERSION.SDK_INT, BuildConfig.VERSION_CODE), token = token @@ -108,10 +107,10 @@ class AppCertManager(private val context: Context) { override fun getHeaders(): Map { return mapOf( - "User-Agent" to "GoogleAuth/1.4 (${Build.DEVICE} ${Build.ID}); gzip", - "content-type" to "application/octet-stream", - "app" to "com.google.android.gms", - "device" to androidId.toString(16) + "User-Agent" to "GoogleAuth/1.4 (${Build.DEVICE} ${Build.ID}); gzip", + "content-type" to "application/octet-stream", + "app" to "com.google.android.gms", + "device" to androidId!!.toString(16) ) } }) @@ -150,7 +149,10 @@ class AppCertManager(private val context: Context) { ) } else { Log.d(TAG, "Using fallback spatula header based on Android ID") - val androidId = getSettings(context, CheckIn.getContentUri(context), arrayOf(CheckIn.ANDROID_ID, CheckIn.SECURITY_TOKEN)) { cursor: Cursor -> cursor.getLong(0) } + val androidId = CheckIn.getContentUri(context)?.let { + getSettings(context, + it, arrayOf(CheckIn.ANDROID_ID, CheckIn.SECURITY_TOKEN)) { cursor: Cursor -> cursor.getLong(0) } + } SpatulaHeaderProto( packageInfo = SpatulaHeaderProto.PackageInfo(packageName, packageCertificateHash), deviceId = androidId diff --git a/play-services-core/src/main/kotlin/org/microg/gms/checkin/CheckinPrefs.kt b/play-services-core/src/main/kotlin/org/microg/gms/checkin/CheckinPrefs.kt index 2953ed60..019ddfa9 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/checkin/CheckinPrefs.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/checkin/CheckinPrefs.kt @@ -12,32 +12,40 @@ import org.microg.mgms.settings.SettingsContract.setSettings object CheckinPrefs { @JvmStatic - fun isEnabled(context: Context): Boolean { + fun isEnabled(context: Context): Boolean? { val projection = arrayOf(CheckIn.ENABLED) - return SettingsContract.getSettings(context, CheckIn.getContentUri(context), projection) { c -> - c.getInt(0) != 0 + return CheckIn.getContentUri(context)?.let { + SettingsContract.getSettings(context, it, projection) { c -> + c.getInt(0) != 0 + } } } @JvmStatic - fun isSpoofingEnabled(context: Context): Boolean { + fun isSpoofingEnabled(context: Context): Boolean? { val projection = arrayOf(CheckIn.BRAND_SPOOF) - return SettingsContract.getSettings(context, CheckIn.getContentUri(context), projection) { c -> - c.getInt(0) != 0 + return CheckIn.getContentUri(context)?.let { + SettingsContract.getSettings(context, it, projection) { c -> + c.getInt(0) != 0 + } } } @JvmStatic fun setSpoofingEnabled(context: Context, enabled: Boolean) { - setSettings(context, CheckIn.getContentUri(context)) { - put(CheckIn.BRAND_SPOOF, enabled) + CheckIn.getContentUri(context)?.let { + setSettings(context, it) { + put(CheckIn.BRAND_SPOOF, enabled) + } } } @JvmStatic fun hideLauncherIcon(context: Context, enabled: Boolean) { - setSettings(context, CheckIn.getContentUri(context)) { - put(CheckIn.HIDE_LAUNCHER_ICON, enabled) + CheckIn.getContentUri(context)?.let { + setSettings(context, it) { + put(CheckIn.HIDE_LAUNCHER_ICON, enabled) + } } } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/checkin/LastCheckinInfo.kt b/play-services-core/src/main/kotlin/org/microg/gms/checkin/LastCheckinInfo.kt index b3056893..e3e26f68 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/checkin/LastCheckinInfo.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/checkin/LastCheckinInfo.kt @@ -39,7 +39,7 @@ data class LastCheckinInfo( companion object { @JvmStatic - fun read(context: Context): LastCheckinInfo { + fun read(context: Context): LastCheckinInfo? { val projection = arrayOf( CheckIn.ANDROID_ID, CheckIn.DIGEST, @@ -48,35 +48,43 @@ data class LastCheckinInfo( CheckIn.VERSION_INFO, CheckIn.DEVICE_DATA_VERSION_INFO, ) - return SettingsContract.getSettings(context, CheckIn.getContentUri(context), projection) { c -> - LastCheckinInfo( - androidId = c.getLong(0), - digest = c.getString(1), - lastCheckin = c.getLong(2), - securityToken = c.getLong(3), - versionInfo = c.getString(4), - deviceDataVersionInfo = c.getString(5), - ) + return CheckIn.getContentUri(context)?.let { + SettingsContract.getSettings(context, it, projection) { c -> + LastCheckinInfo( + androidId = c.getLong(0), + digest = c.getString(1), + lastCheckin = c.getLong(2), + securityToken = c.getLong(3), + versionInfo = c.getString(4), + deviceDataVersionInfo = c.getString(5), + ) + } } } @JvmStatic - fun clear(context: Context) = SettingsContract.setSettings(context, CheckIn.getContentUri(context)) { - put(CheckIn.ANDROID_ID, 0L) - put(CheckIn.DIGEST, CheckIn.INITIAL_DIGEST) - put(CheckIn.LAST_CHECK_IN, 0L) - put(CheckIn.SECURITY_TOKEN, 0L) - put(CheckIn.VERSION_INFO, "") - put(CheckIn.DEVICE_DATA_VERSION_INFO, "") - } + fun clear(context: Context) = + CheckIn.getContentUri(context)?.let { + SettingsContract.setSettings(context, it) { + put(CheckIn.ANDROID_ID, 0L) + put(CheckIn.DIGEST, CheckIn.INITIAL_DIGEST) + put(CheckIn.LAST_CHECK_IN, 0L) + put(CheckIn.SECURITY_TOKEN, 0L) + put(CheckIn.VERSION_INFO, "") + put(CheckIn.DEVICE_DATA_VERSION_INFO, "") + } + } } - fun write(context: Context) = SettingsContract.setSettings(context, CheckIn.getContentUri(context)) { - put(CheckIn.ANDROID_ID, androidId) - put(CheckIn.DIGEST, digest) - put(CheckIn.LAST_CHECK_IN, lastCheckin) - put(CheckIn.SECURITY_TOKEN, securityToken) - put(CheckIn.VERSION_INFO, versionInfo) - put(CheckIn.DEVICE_DATA_VERSION_INFO, deviceDataVersionInfo) - } + fun write(context: Context) = + CheckIn.getContentUri(context)?.let { + SettingsContract.setSettings(context, it) { + put(CheckIn.ANDROID_ID, androidId) + put(CheckIn.DIGEST, digest) + put(CheckIn.LAST_CHECK_IN, lastCheckin) + put(CheckIn.SECURITY_TOKEN, securityToken) + put(CheckIn.VERSION_INFO, versionInfo) + put(CheckIn.DEVICE_DATA_VERSION_INFO, deviceDataVersionInfo) + } + } } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/checkin/ServiceInfo.kt b/play-services-core/src/main/kotlin/org/microg/gms/checkin/ServiceInfo.kt index 1637643b..67084e03 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/checkin/ServiceInfo.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/checkin/ServiceInfo.kt @@ -20,22 +20,26 @@ data class ServiceConfiguration(val enabled: Boolean) : Serializable suspend fun getCheckinServiceInfo(context: Context): ServiceInfo = withContext(Dispatchers.IO) { val projection = arrayOf(CheckIn.ENABLED, CheckIn.LAST_CHECK_IN, CheckIn.ANDROID_ID) - getSettings(context, CheckIn.getContentUri(context), projection) { c -> - ServiceInfo( - configuration = ServiceConfiguration(c.getInt(0) != 0), - lastCheckin = c.getLong(1), - androidId = c.getLong(2), - ) - } + CheckIn.getContentUri(context)?.let { + getSettings(context, it, projection) { c -> + ServiceInfo( + configuration = ServiceConfiguration(c.getInt(0) != 0), + lastCheckin = c.getLong(1), + androidId = c.getLong(2), + ) + } + }!! } suspend fun setCheckinServiceConfiguration(context: Context, configuration: ServiceConfiguration) = withContext(Dispatchers.IO) { val serviceInfo = getCheckinServiceInfo(context) if (serviceInfo.configuration == configuration) return@withContext // enabled state is not already set, setting it now - setSettings(context, CheckIn.getContentUri(context)) { + CheckIn.getContentUri(context)?.let { + setSettings(context, it) { put(CheckIn.ENABLED, configuration.enabled) } + } if (configuration.enabled) { context.sendOrderedBroadcast(Intent(context, TriggerReceiver::class.java), null) } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmPrefs.kt b/play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmPrefs.kt index b2402779..77cf34b0 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmPrefs.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/gcm/GcmPrefs.kt @@ -38,39 +38,44 @@ data class GcmPrefs( const val PREF_NETWORK_OTHER = Gcm.NETWORK_OTHER @JvmStatic - fun get(context: Context): GcmPrefs { - return SettingsContract.getSettings(context, Gcm.getContentUri(context), Gcm.PROJECTION) { c -> - GcmPrefs( - isGcmLogEnabled = c.getInt(0) != 0, - lastPersistedId = c.getString(1), - gcmEnabled = c.getInt(2) != 0, - networkMobile = c.getInt(3), - networkWifi = c.getInt(4), - networkRoaming = c.getInt(5), - networkOther = c.getInt(6), - learntMobileInterval = c.getInt(7), - learntWifiInterval = c.getInt(8), - learntOtherInterval = c.getInt(9), - ) + fun get(context: Context): GcmPrefs? { + return Gcm.getContentUri(context)?.let { + SettingsContract.getSettings(context, it, Gcm.PROJECTION) { c -> + GcmPrefs( + isGcmLogEnabled = c.getInt(0) != 0, + lastPersistedId = c.getString(1), + gcmEnabled = c.getInt(2) != 0, + networkMobile = c.getInt(3), + networkWifi = c.getInt(4), + networkRoaming = c.getInt(5), + networkOther = c.getInt(6), + learntMobileInterval = c.getInt(7), + learntWifiInterval = c.getInt(8), + learntOtherInterval = c.getInt(9), + ) + } } } fun write(context: Context, config: ServiceConfiguration) { - val gcmPrefs = get(context) - setSettings(context, Gcm.getContentUri(context)) { - put(Gcm.ENABLE_GCM, config.enabled) - put(Gcm.NETWORK_MOBILE, config.mobile) - put(Gcm.NETWORK_WIFI, config.wifi) - put(Gcm.NETWORK_ROAMING, config.roaming) - put(Gcm.NETWORK_OTHER, config.other) + Gcm.getContentUri(context)?.let { + setSettings(context, it) { + put(Gcm.ENABLE_GCM, config.enabled) + put(Gcm.NETWORK_MOBILE, config.mobile) + put(Gcm.NETWORK_WIFI, config.wifi) + put(Gcm.NETWORK_ROAMING, config.roaming) + put(Gcm.NETWORK_OTHER, config.other) + } } - gcmPrefs.setEnabled(context, config.enabled) + get(context)?.setEnabled(context, config.enabled) } @JvmStatic fun clearLastPersistedId(context: Context) { - setSettings(context, Gcm.getContentUri(context)) { - put(Gcm.LAST_PERSISTENT_ID, "") + Gcm.getContentUri(context)?.let { + setSettings(context, it) { + put(Gcm.LAST_PERSISTENT_ID, "") + } } } } @@ -116,17 +121,23 @@ data class GcmPrefs( fun learnTimeout(context: Context, pref: String) { Log.d("GmsGcmPrefs", "learnTimeout: $pref") when (pref) { - PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> setSettings(context, Gcm.getContentUri(context)) { - val newInterval = (learntMobileInterval * 0.95).toInt() - put(Gcm.LEARNT_MOBILE, newInterval) + PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> Gcm.getContentUri(context)?.let { + setSettings(context, it) { + val newInterval = (learntMobileInterval * 0.95).toInt() + put(Gcm.LEARNT_MOBILE, newInterval) + } } - PREF_NETWORK_WIFI -> setSettings(context, Gcm.getContentUri(context)) { - val newInterval = (learntMobileInterval * 0.95).toInt() - put(Gcm.LEARNT_WIFI, newInterval) + PREF_NETWORK_WIFI -> Gcm.getContentUri(context)?.let { + setSettings(context, it) { + val newInterval = (learntMobileInterval * 0.95).toInt() + put(Gcm.LEARNT_WIFI, newInterval) + } } - else -> setSettings(context, Gcm.getContentUri(context)) { - val newInterval = (learntMobileInterval * 0.95).toInt() - put(Gcm.LEARNT_OTHER, newInterval) + else -> Gcm.getContentUri(context)?.let { + setSettings(context, it) { + val newInterval = (learntMobileInterval * 0.95).toInt() + put(Gcm.LEARNT_OTHER, newInterval) + } } } } @@ -136,22 +147,28 @@ data class GcmPrefs( when (pref) { PREF_NETWORK_MOBILE, PREF_NETWORK_ROAMING -> { if (time > learntMobileInterval / 4 * 3) { - setSettings(context, Gcm.getContentUri(context)) { - put(Gcm.LEARNT_MOBILE, SettingsProvider.INTERVAL) + Gcm.getContentUri(context)?.let { + setSettings(context, it) { + put(Gcm.LEARNT_MOBILE, SettingsProvider.INTERVAL) + } } } } PREF_NETWORK_WIFI -> { if (time > learntWifiInterval / 4 * 3) { - setSettings(context, Gcm.getContentUri(context)) { - put(Gcm.LEARNT_WIFI, SettingsProvider.INTERVAL) + Gcm.getContentUri(context)?.let { + setSettings(context, it) { + put(Gcm.LEARNT_WIFI, SettingsProvider.INTERVAL) + } } } } else -> { if (time > learntOtherInterval / 4 * 3) { - setSettings(context, Gcm.getContentUri(context)) { - put(Gcm.LEARNT_OTHER, SettingsProvider.INTERVAL) + Gcm.getContentUri(context)?.let { + setSettings(context, it) { + put(Gcm.LEARNT_OTHER, SettingsProvider.INTERVAL) + } } } } @@ -164,8 +181,10 @@ data class GcmPrefs( fun extendLastPersistedId(context: Context, id: String) { val newId = if (lastPersistedId.isNullOrEmpty()) id else "$lastPersistedId|$id" - setSettings(context, Gcm.getContentUri(context)) { - put(Gcm.LAST_PERSISTENT_ID, newId) + Gcm.getContentUri(context)?.let { + setSettings(context, it) { + put(Gcm.LAST_PERSISTENT_ID, newId) + } } } diff --git a/play-services-core/src/main/kotlin/org/microg/gms/gcm/PushRegisterService.kt b/play-services-core/src/main/kotlin/org/microg/gms/gcm/PushRegisterService.kt index d3e519bd..4b7b44bc 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/gcm/PushRegisterService.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/gcm/PushRegisterService.kt @@ -6,6 +6,7 @@ package org.microg.gms.gcm +import android.annotation.SuppressLint import android.app.PendingIntent import android.content.Context import android.content.Intent @@ -29,30 +30,32 @@ import kotlin.coroutines.suspendCoroutine private const val TAG = "GmsGcmRegister" private suspend fun ensureCheckinIsUpToDate(context: Context) { - if (!CheckinPrefs.isEnabled(context)) throw RuntimeException("Checkin disabled") - val lastCheckin = LastCheckinInfo.read(context).lastCheckin - if (lastCheckin < System.currentTimeMillis() - CheckinService.MAX_VALID_CHECKIN_AGE) { - val resultData: Bundle = suspendCoroutine { continuation -> - val intent = Intent(context, CheckinService::class.java) - val continued = AtomicBoolean(false) - intent.putExtra(CheckinService.EXTRA_RESULT_RECEIVER, object : ResultReceiver(null) { - override fun onReceiveResult(resultCode: Int, resultData: Bundle?) { - if (continued.compareAndSet(false, true)) continuation.resume(resultData ?: Bundle.EMPTY) - } - }) - ForegroundServiceContext(context).startService(intent) - Handler().postDelayed({ - if (continued.compareAndSet(false, true)) continuation.resume(Bundle.EMPTY) - }, 10000L) - } - if (resultData.getLong(CheckinService.EXTRA_NEW_CHECKIN_TIME, 0L) + lastCheckin == 0L) { - throw RuntimeException("No checkin available") + if (!CheckinPrefs.isEnabled(context)!!) throw RuntimeException("Checkin disabled") + val lastCheckin = LastCheckinInfo.read(context)?.lastCheckin + if (lastCheckin != null) { + if (lastCheckin < System.currentTimeMillis() - CheckinService.MAX_VALID_CHECKIN_AGE) { + val resultData: Bundle = suspendCoroutine { continuation -> + val intent = Intent(context, CheckinService::class.java) + val continued = AtomicBoolean(false) + intent.putExtra(CheckinService.EXTRA_RESULT_RECEIVER, object : ResultReceiver(null) { + override fun onReceiveResult(resultCode: Int, resultData: Bundle?) { + if (continued.compareAndSet(false, true)) continuation.resume(resultData ?: Bundle.EMPTY) + } + }) + ForegroundServiceContext(context).startService(intent) + Handler().postDelayed({ + if (continued.compareAndSet(false, true)) continuation.resume(Bundle.EMPTY) + }, 10000L) + } + if (resultData.getLong(CheckinService.EXTRA_NEW_CHECKIN_TIME, 0L) + lastCheckin == 0L) { + throw RuntimeException("No checkin available") + } } } } -private suspend fun ensureAppRegistrationAllowed(context: Context, database: GcmDatabase, packageName: String) { - if (!GcmPrefs.get(context).isEnabled) throw RuntimeException("GCM disabled") +private fun ensureAppRegistrationAllowed(context: Context, database: GcmDatabase, packageName: String) { + if (!GcmPrefs.get(context)?.isEnabled!!) throw RuntimeException("GCM disabled") val app = database.getApp(packageName) if (app?.allowRegister == false) { throw RuntimeException("Push permission not granted to $packageName") @@ -65,7 +68,7 @@ suspend fun completeRegisterRequest(context: Context, database: GcmDatabase, req private val Intent.requestId: String? get() { - val kidString = getStringExtra(GcmConstants.EXTRA_KID) ?: return null + val kidString = getStringExtra(EXTRA_KID) ?: return null if (kidString.startsWith("|")) { val kid = kidString.split("\\|".toRegex()).toTypedArray() if (kid.size >= 3 && "ID" == kid[1]) { @@ -242,6 +245,7 @@ internal class PushRegisterHandler(private val context: Context, private val dat } private val selfAuthIntent: PendingIntent + @SuppressLint("UnspecifiedImmutableFlag") get() { val intent = Intent() intent.setPackage("com.google.example.invalidpackage") diff --git a/play-services-core/src/main/kotlin/org/microg/gms/gcm/ServiceInfo.kt b/play-services-core/src/main/kotlin/org/microg/gms/gcm/ServiceInfo.kt index be9c0f67..1fd34bac 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/gcm/ServiceInfo.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/gcm/ServiceInfo.kt @@ -36,14 +36,16 @@ class ServiceInfoReceiver : BroadcastReceiver() { context.sendOrderedBroadcast(Intent(ACTION_SERVICE_INFO_RESPONSE).apply { setPackage(context.packageName) val prefs = GcmPrefs.get(context) - val info = ServiceInfo( - configuration = prefs.toConfiguration(), + val info = prefs?.let { + ServiceInfo( + configuration = it.toConfiguration(), connected = McsService.isConnected(context), startTimestamp = McsService.getStartTimestamp(), learntMobileInterval = prefs.learntMobileInterval, learntWifiInterval = prefs.learntWifiInterval, learntOtherInterval = prefs.learntOtherInterval - ) + ) + } putExtra(EXTRA_SERVICE_INFO, info) }, null) } catch (e: Exception) { diff --git a/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationPreferencesFragment.kt b/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationPreferencesFragment.kt index 1a5caae1..ef027a84 100755 --- a/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationPreferencesFragment.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/ui/PushNotificationPreferencesFragment.kt @@ -72,14 +72,14 @@ class PushNotificationPreferencesFragment : PreferenceFragmentCompat() { val appContext = requireContext().applicationContext lifecycleScope.launchWhenStarted { val statusInfo = getGcmServiceInfo(appContext) - pushStatusCategory.isVisible = statusInfo != null && statusInfo.configuration.enabled + pushStatusCategory.isVisible = statusInfo.configuration.enabled pushStatus.summary = if (statusInfo.connected) { appContext.getString(R.string.gcm_network_state_connected, DateUtils.getRelativeTimeSpanString(statusInfo.startTimestamp, System.currentTimeMillis(), 0)) } else { appContext.getString(R.string.gcm_network_state_disconnected) } } - } catch (e: Exception) {} + } catch (_: Exception) {} } private fun updateContent() { diff --git a/play-services-core/src/main/kotlin/org/microg/gms/ui/SettingsFragment.kt b/play-services-core/src/main/kotlin/org/microg/gms/ui/SettingsFragment.kt index 848e702b..8d503a68 100644 --- a/play-services-core/src/main/kotlin/org/microg/gms/ui/SettingsFragment.kt +++ b/play-services-core/src/main/kotlin/org/microg/gms/ui/SettingsFragment.kt @@ -69,7 +69,7 @@ class SettingsFragment : ResourceSettingsFragment() { findPreference(PREF_GCM)?.setSummary(R.string.service_status_disabled_short) } - findPreference(PREF_CHECKIN)?.setSummary(if (CheckinPrefs.isEnabled(context)) R.string.service_status_enabled_short else R.string.service_status_disabled_short) + findPreference(PREF_CHECKIN)?.setSummary(if (CheckinPrefs.isEnabled(context) == true) R.string.service_status_enabled_short else R.string.service_status_disabled_short) } companion object {