Skip to content

Commit

Permalink
Handle clicks on user intervention actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fynngodau committed Apr 9, 2024
1 parent cd21824 commit 283f717
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 46 deletions.
3 changes: 3 additions & 0 deletions play-services-core/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,9 @@
<intent-filter>
<action android:name="android.intent.action.APPLICATION_PREFERENCES" />
<action android:name="com.google.android.gms.settings.EXPOSURE_NOTIFICATION_SETTINGS" />
<action android:name="org.microg.gms.settings.CHECKIN_SETTINGS" />
<action android:name="org.microg.gms.settings.GCM_SETTINGS" />
<action android:name="org.microg.gms.settings.MICROG_APP_GCM_SETTINGS" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
package org.microg.gms.ui;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.StyleSpan;
import android.view.View;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.google.android.gms.R;

import org.microg.gms.gcm.GcmDatabase;
import org.microg.gms.gcm.PushRegisterService;

import static org.microg.gms.gcm.GcmConstants.EXTRA_APP;
import static org.microg.gms.gcm.GcmConstants.EXTRA_KID;
import static org.microg.gms.gcm.GcmConstants.EXTRA_PENDING_INTENT;

public class AskPushPermission extends FragmentActivity {
public static final String EXTRA_REQUESTED_PACKAGE = "package";
public static final String EXTRA_RESULT_RECEIVER = "receiver";
public static final String EXTRA_FORCE_ASK = "force";
public static final String EXTRA_EXPLICIT = "explicit";

private GcmDatabase database;
Expand All @@ -47,13 +37,14 @@ public void onCreate(Bundle savedInstanceState) {

packageName = getIntent().getStringExtra(EXTRA_REQUESTED_PACKAGE);
resultReceiver = getIntent().getParcelableExtra(EXTRA_RESULT_RECEIVER);
if (packageName == null || resultReceiver == null) {
boolean force = getIntent().getBooleanExtra(EXTRA_FORCE_ASK, false);
if (packageName == null || (resultReceiver == null && !force)) {
answered = true;
finish();
return;
}

if (database.getApp(packageName) != null) {
if (!force && database.getApp(packageName) != null) {
resultReceiver.send(Activity.RESULT_OK, Bundle.EMPTY);
answered = true;
finish();
Expand All @@ -71,29 +62,23 @@ public void onCreate(Bundle savedInstanceState) {
s.setSpan(new StyleSpan(Typeface.BOLD), raw.indexOf(label), raw.indexOf(label) + label.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);

((TextView) findViewById(R.id.permission_message)).setText(s);
findViewById(R.id.permission_allow_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answered) return;
database.noteAppKnown(packageName, true);
answered = true;
Bundle bundle = new Bundle();
bundle.putBoolean(EXTRA_EXPLICIT, true);
resultReceiver.send(Activity.RESULT_OK, bundle);
finish();
}
findViewById(R.id.permission_allow_button).setOnClickListener(v -> {
if (answered) return;
database.noteAppKnown(packageName, true);
answered = true;
Bundle bundle = new Bundle();
bundle.putBoolean(EXTRA_EXPLICIT, true);
if (resultReceiver != null) resultReceiver.send(Activity.RESULT_OK, bundle);
finish();
});
findViewById(R.id.permission_deny_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (answered) return;
database.noteAppKnown(packageName, false);
answered = true;
Bundle bundle = new Bundle();
bundle.putBoolean(EXTRA_EXPLICIT, true);
resultReceiver.send(Activity.RESULT_CANCELED, bundle);
finish();
}
findViewById(R.id.permission_deny_button).setOnClickListener(v -> {
if (answered) return;
database.noteAppKnown(packageName, false);
answered = true;
Bundle bundle = new Bundle();
bundle.putBoolean(EXTRA_EXPLICIT, true);
if (resultReceiver != null) resultReceiver.send(Activity.RESULT_CANCELED, bundle);
finish();
});
} catch (PackageManager.NameNotFoundException e) {
finish();
Expand All @@ -104,7 +89,7 @@ public void onClick(View v) {
protected void onDestroy() {
super.onDestroy();
if (!answered) {
resultReceiver.send(Activity.RESULT_CANCELED, Bundle.EMPTY);
if (resultReceiver != null) resultReceiver.send(Activity.RESULT_CANCELED, Bundle.EMPTY);
}
database.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ fun Context.resolveAuthErrorMessage(s: String): Resolution? = if (s.startsWith("
SettingsContract.CheckIn.LAST_CHECK_IN
)
SettingsContract.getSettings(this, SettingsContract.CheckIn.getContentUri(this), settingsProjection) { cursor ->
//val checkInEnabled = cursor.getInt(0) != 0
val checkInEnabled = cursor.getInt(0) != 0
val lastCheckIn = cursor.getLong(1)

if (lastCheckIn <= 0) {
if (lastCheckIn <= 0 || !checkInEnabled) {
// user is also asked to enable checkin if there had never been a successful checkin (network errors?)
actions += UserAction.ENABLE_CHECKIN
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package org.microg.gms.accountaction

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.ResultReceiver
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
Expand All @@ -21,49 +26,78 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.google.android.gms.R
import org.microg.gms.accountaction.UserAction.*
import org.microg.gms.common.Constants
import org.microg.gms.ui.AskPushPermission
import kotlin.coroutines.resume

const val ACTION_CHECKIN = "org.microg.gms.settings.DEVICE_REGISTRATION_SETTINGS"
const val ACTION_GCM = "org.microg.gms.settings.GCM_SETTINGS"
const val ACTION_GCM_APP = "org.microg.gms.settings.GCM_APP_SETTINGS"
const val URI_GCM_MICROG_APP = "x-gms-settings://gcm/${Constants.GMS_PACKAGE_NAME}"

@Composable
fun UserInterventionComponents(userActions: Map<UserAction, Boolean>) {
for ((index, action) in userActions.entries.withIndex()) {
val context = LocalContext.current as Activity
when (action.component1()) {
ENABLE_CHECKIN -> UserInterventionCommonComponent(
title = stringResource(id = R.string.auth_action_step_enable_checkin),
description = stringResource(id = R.string.auth_action_step_enable_checkin_description),
sequenceNumber = index + 1,
completed = action.component2()
)
) {
Intent(ACTION_CHECKIN).let { context.startActivityForResult(it, 0) }
}
ENABLE_GCM -> UserInterventionCommonComponent(
title = stringResource(id = R.string.auth_action_step_enable_gcm),
description = stringResource(id = R.string.auth_action_step_enable_gcm_description),
sequenceNumber = index + 1,
completed = action.component2()
)
) {
Intent(ACTION_GCM).let { context.startActivityForResult(it, 1) }
}
ALLOW_MICROG_GCM -> UserInterventionCommonComponent(
title = stringResource(id = R.string.auth_action_step_allow_microg_gcm),
description = stringResource(id = R.string.auth_action_step_allow_microg_gcm_description),
sequenceNumber = index + 1,
completed = action.component2()
)
) {
Intent(context, AskPushPermission::class.java).apply {
putExtra(AskPushPermission.EXTRA_REQUESTED_PACKAGE, Constants.GMS_PACKAGE_NAME)
putExtra(AskPushPermission.EXTRA_FORCE_ASK, true)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS)
addFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
}.let { context.startActivity(it) }
}
ENABLE_LOCKSCREEN -> UserInterventionCommonComponent(
title = stringResource(id = R.string.auth_action_step_enable_lockscreen),
description = stringResource(id = R.string.auth_action_step_enable_lockscreen_description),
sequenceNumber = index + 1,
completed = action.component2()
)
) {
runCatching {
Intent(android.provider.Settings.ACTION_SECURITY_SETTINGS).let { context.startActivity(it) }
}.onFailure {
Intent(android.provider.Settings.ACTION_SETTINGS).let { context.startActivity(it) }
}

}
REAUTHENTICATE -> TODO()
}
}
}

@Composable
fun UserInterventionCommonComponent(title: String, description: String, sequenceNumber: Int?, completed: Boolean) {
Surface(onClick = { /*TODO*/ }) {
fun UserInterventionCommonComponent(title: String, description: String, sequenceNumber: Int?, completed: Boolean, onClick: () -> Unit) {
Surface(onClick = onClick, enabled = !completed) {

val color = if (completed) {
colorResource(id = R.color.material_success)
Expand Down
5 changes: 4 additions & 1 deletion play-services-core/src/main/res/layout/ask_gcm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_height="match_parent"
android:minWidth="240dp"
tools:theme="@style/Theme.App.DayNight.Dialog.Alert.NoActionBar">

<LinearLayout
android:id="@+id/dialog_container"
Expand Down
9 changes: 8 additions & 1 deletion play-services-core/src/main/res/navigation/nav_settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@
android:id="@+id/checkinFragment"
android:name="org.microg.gms.ui.DeviceRegistrationFragment"
android:label="@string/service_name_checkin"
tools:layout="@layout/device_registration_fragment" />
tools:layout="@layout/device_registration_fragment">
<deepLink
app:uri="x-gms-settings://checkin"
app:action="org.microg.gms.settings.CHECKIN_SETTINGS" />
</fragment>

<!-- Push Notifications -->

Expand All @@ -75,6 +79,9 @@
<action
android:id="@+id/openGcmAdvancedSettings"
app:destination="@id/gcmAdvancedFragment" />
<deepLink
app:uri="x-gms-settings://gcm"
app:action="org.microg.gms.settings.GCM_SETTINGS" />
</fragment>

<fragment
Expand Down

0 comments on commit 283f717

Please sign in to comment.