Skip to content

Commit

Permalink
DeviceRegistrationFragment: Allow viewing device registration data sh…
Browse files Browse the repository at this point in the history
…ared with remote

Change-Id: I93666445d14505c0a4289d63caa8054440ef1eef
Signed-off-by: Aayush Gupta <[email protected]>
  • Loading branch information
theimpulson committed Jul 30, 2024
1 parent de40837 commit 39b3729
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 3 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ buildscript {
ext.biometricVersion = '1.1.0'
ext.coreVersion = '1.12.0'
ext.fragmentVersion = '1.6.2'
ext.gsonVersion = '2.10.1'
ext.lifecycleVersion = '2.7.0'
ext.loaderVersion = '1.1.0'
ext.materialVersion = '1.11.0'
Expand Down
3 changes: 3 additions & 0 deletions play-services-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ dependencies {
implementation "androidx.preference:preference-ktx:$preferenceVersion"
implementation "androidx.webkit:webkit:$webkitVersion"

// Gson
implementation "com.google.code.gson:gson:$gsonVersion"

// Material Components
implementation "com.google.android.material:material:$materialVersion"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;

import androidx.annotation.Nullable;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import org.microg.gms.auth.AuthConstants;
import org.microg.gms.auth.AuthRequest;
Expand All @@ -33,6 +39,10 @@
import java.util.List;

public class CheckinManager {

private static final String LAST_CHECK_IN_DATA = "LAST_CHECK_IN_DATA";
private static final String CHECK_IN_PREF = "check_in";

private static final long MIN_CHECKIN_INTERVAL = 3 * 60 * 60 * 1000; // 3 hours

@SuppressWarnings("MissingPermission")
Expand All @@ -42,7 +52,20 @@ public static synchronized LastCheckinInfo checkin(Context context, boolean forc
return null;
if (!CheckinPreferences.isEnabled(context))
return null;
List<CheckinClient.Account> accounts = new ArrayList<CheckinClient.Account>();
CheckinRequest checkinRequest = getCheckinRequest(context, info);
saveLastCheckInRequest(context, checkinRequest);
return handleResponse(context, CheckinClient.request(checkinRequest));
}

@Nullable
public static String getLastRawCheckInRequest(Context context) {
String rawCheckInRequest = getCheckInSharedPreferences(context)
.getString(LAST_CHECK_IN_DATA, "");
return !rawCheckInRequest.isEmpty() ? rawCheckInRequest : null;
}

private static CheckinRequest getCheckinRequest(Context context, LastCheckinInfo info) throws IOException {
List<CheckinClient.Account> accounts = new ArrayList<>();
AccountManager accountManager = AccountManager.get(context);
String accountType = AuthConstants.DEFAULT_ACCOUNT_TYPE;
for (Account account : accountManager.getAccountsByType(accountType)) {
Expand All @@ -55,10 +78,9 @@ public static synchronized LastCheckinInfo checkin(Context context, boolean forc
accounts.add(new CheckinClient.Account(account.name, token));
}
}
CheckinRequest request = CheckinClient.makeRequest(context,
return CheckinClient.makeRequest(context,
new DeviceConfiguration(context), Utils.getDeviceIdentifier(context),
Utils.getPhoneInfo(context), info, Utils.getLocale(context), accounts);
return handleResponse(context, CheckinClient.request(request));
}

private static LastCheckinInfo handleResponse(Context context, CheckinResponse response) {
Expand All @@ -72,4 +94,20 @@ private static LastCheckinInfo handleResponse(Context context, CheckinResponse r

return info;
}

private static void saveLastCheckInRequest(Context context, CheckinRequest checkinRequest) {
getCheckInSharedPreferences(context).edit()
.putString(LAST_CHECK_IN_DATA, getGsonInstance().toJson(checkinRequest))
.apply();
}

private static SharedPreferences getCheckInSharedPreferences(Context context) {
return context.getSharedPreferences(CHECK_IN_PREF, Context.MODE_PRIVATE);
}

private static Gson getGsonInstance() {
return new GsonBuilder()
.setPrettyPrinting()
.create();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
import com.google.android.gms.R
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import org.microg.gms.checkin.CheckinManager
import org.microg.gms.checkin.CheckinPreferences
import org.microg.gms.checkin.getCheckinServiceInfo
import org.microg.gms.profile.ProfileManager
Expand Down Expand Up @@ -94,6 +96,17 @@ class DeviceRegistrationFragment : PreferenceFragmentCompat() {
CheckinPreferences.setEnabled(requireContext(), newStatus)
true
}

findPreference<Preference>("pref_device_registration_data")?.setOnPreferenceClickListener {
val rawCheckInRequest = CheckinManager.getLastRawCheckInRequest(context)
MaterialAlertDialogBuilder(it.context)
.setTitle(R.string.pref_device_registration_data_title)
.setMessage(rawCheckInRequest ?: getString(R.string.data_na))
.setPositiveButton(android.R.string.ok) { dialog, _ -> dialog.dismiss() }
.create()
.show()
true
}
}

private fun configureProfilePreference() {
Expand Down
4 changes: 4 additions & 0 deletions play-services-core/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ This can take a couple of minutes."</string>
<string name="self_check_resolution_battery_optimizations">Touch here to disable battery optimizations. Not doing this may result in misbehaving applications.</string>

<!-- Settings strings -->
<string name="data_na">No data available right now</string>

<string name="prefcat_about">About</string>
<string name="prefcat_components">Components</string>
Expand Down Expand Up @@ -146,6 +147,9 @@ This can take a couple of minutes."</string>
<string name="pref_checkin_enable_summary">Registers your device to Google services and creates a unique device identifier. microG strips identifying bits other than your Google account name from registration data.</string>
<string name="pref_device_registration_android_id">Android ID</string>

<string name="pref_device_registration_data_title">Device registration data</string>
<string name="pref_device_registration_data_summary">Check data shared with Google servers for device registration</string>

<string name="checkin_not_registered">Not registered</string>
<string name="checkin_last_registration">Last registration: <xliff:g example="Yesterday, 02:20 PM">%1$s</xliff:g></string>
<string name="checkin_enable_switch">Register device</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
android:title="@string/pref_device_registration_android_id"
tools:summary="1953a59d1c1b7e4b"
app:iconSpaceReserved="false" />
<Preference
android:key="pref_device_registration_data"
android:summary="@string/pref_device_registration_data_summary"
android:title="@string/pref_device_registration_data_title"
app:iconSpaceReserved="false" />
</PreferenceCategory>
<PreferenceCategory android:layout="@layout/preference_category_no_label">
<org.microg.gms.ui.FooterPreference
Expand Down

0 comments on commit 39b3729

Please sign in to comment.