mirror of
https://github.com/signalapp/Signal-Android.git
synced 2026-02-15 07:28:30 +00:00
Move several Permissions dependencies to core.
This commit is contained in:
@@ -37,4 +37,5 @@ dependencies {
|
||||
implementation(libs.kotlinx.serialization.json)
|
||||
api(libs.google.zxing.core)
|
||||
api(libs.material.material)
|
||||
api(libs.accompanist.permissions)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2024 Signal Messenger, LLC
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
*/
|
||||
|
||||
package org.signal.core.ui.compose
|
||||
|
||||
import android.Manifest
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.runtime.setValue
|
||||
import androidx.compose.ui.graphics.painter.Painter
|
||||
import androidx.compose.ui.res.stringResource
|
||||
import com.google.accompanist.permissions.ExperimentalPermissionsApi
|
||||
import com.google.accompanist.permissions.isGranted
|
||||
import com.google.accompanist.permissions.rememberPermissionState
|
||||
import org.signal.core.ui.R
|
||||
|
||||
/**
|
||||
* Dialogs and state management for permissions requests in compose screens.
|
||||
*/
|
||||
object Permissions {
|
||||
|
||||
interface Controller {
|
||||
fun request()
|
||||
}
|
||||
|
||||
private enum class RequestState {
|
||||
NONE,
|
||||
RATIONALE,
|
||||
SYSTEM
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun cameraPermissionHandler(
|
||||
rationale: String,
|
||||
onPermissionGranted: () -> Unit
|
||||
): Controller {
|
||||
return permissionHandler(
|
||||
permission = Manifest.permission.CAMERA,
|
||||
icon = SignalIcons.Camera.painter,
|
||||
rationale = rationale,
|
||||
onPermissionGranted = onPermissionGranted
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Generic permissions rationale dialog and state management for single permissions.
|
||||
*/
|
||||
@OptIn(ExperimentalPermissionsApi::class)
|
||||
@Composable
|
||||
fun permissionHandler(
|
||||
permission: String,
|
||||
icon: Painter,
|
||||
rationale: String,
|
||||
onPermissionGranted: () -> Unit
|
||||
): Controller {
|
||||
var requestState by remember {
|
||||
mutableStateOf(RequestState.NONE)
|
||||
}
|
||||
|
||||
val permissionState = rememberPermissionState(permission = permission) {
|
||||
if (it && requestState == RequestState.SYSTEM) {
|
||||
onPermissionGranted()
|
||||
}
|
||||
}
|
||||
|
||||
if (requestState == RequestState.RATIONALE) {
|
||||
Dialogs.PermissionRationaleDialog(
|
||||
icon = icon,
|
||||
rationale = rationale,
|
||||
confirm = stringResource(id = R.string.Permissions_continue),
|
||||
dismiss = stringResource(id = R.string.Permissions_not_now),
|
||||
onConfirm = {
|
||||
requestState = RequestState.SYSTEM
|
||||
permissionState.launchPermissionRequest()
|
||||
},
|
||||
onDismiss = {
|
||||
requestState = RequestState.NONE
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
return object : Controller {
|
||||
override fun request() {
|
||||
if (permissionState.status.isGranted) {
|
||||
requestState = RequestState.NONE
|
||||
onPermissionGranted()
|
||||
} else {
|
||||
requestState = RequestState.RATIONALE
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
102
core/ui/src/main/java/org/signal/core/ui/util/ThemeUtil.java
Normal file
102
core/ui/src/main/java/org/signal/core/ui/util/ThemeUtil.java
Normal file
@@ -0,0 +1,102 @@
|
||||
package org.signal.core.ui.util;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.util.TypedValue;
|
||||
import android.view.LayoutInflater;
|
||||
|
||||
import androidx.annotation.AttrRes;
|
||||
import androidx.annotation.ColorInt;
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.annotation.StyleRes;
|
||||
import androidx.appcompat.content.res.AppCompatResources;
|
||||
import androidx.appcompat.view.ContextThemeWrapper;
|
||||
|
||||
import org.signal.core.ui.R;
|
||||
|
||||
public class ThemeUtil {
|
||||
|
||||
public static boolean isDarkNotificationTheme(@NonNull Context context) {
|
||||
return (context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
|
||||
}
|
||||
|
||||
public static boolean isDarkTheme(@NonNull Context context) {
|
||||
return getAttribute(context, R.attr.theme_type, "light").equals("dark");
|
||||
}
|
||||
|
||||
public static int getThemedResourceId(@NonNull Context context, @AttrRes int attr) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
Resources.Theme theme = context.getTheme();
|
||||
|
||||
if (theme.resolveAttribute(attr, typedValue, true)) {
|
||||
return typedValue.resourceId;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static boolean getThemedBoolean(@NonNull Context context, @AttrRes int attr) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
Resources.Theme theme = context.getTheme();
|
||||
|
||||
if (theme.resolveAttribute(attr, typedValue, true)) {
|
||||
return typedValue.data != 0;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public static @ColorInt int getThemedColor(@NonNull Context context, @AttrRes int attr) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
Resources.Theme theme = context.getTheme();
|
||||
|
||||
if (theme.resolveAttribute(attr, typedValue, true)) {
|
||||
return typedValue.data;
|
||||
}
|
||||
return Color.RED;
|
||||
}
|
||||
|
||||
public static @Nullable Drawable getThemedDrawable(@NonNull Context context, @AttrRes int attr) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
Resources.Theme theme = context.getTheme();
|
||||
|
||||
if (theme.resolveAttribute(attr, typedValue, true)) {
|
||||
return AppCompatResources.getDrawable(context, typedValue.resourceId);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
public static LayoutInflater getThemedInflater(@NonNull Context context, @NonNull LayoutInflater inflater, @StyleRes int theme) {
|
||||
Context contextThemeWrapper = new ContextThemeWrapper(context, theme);
|
||||
return inflater.cloneInContext(contextThemeWrapper);
|
||||
}
|
||||
|
||||
public static float getThemedDimen(@NonNull Context context, @AttrRes int attr) {
|
||||
TypedValue typedValue = new TypedValue();
|
||||
Resources.Theme theme = context.getTheme();
|
||||
|
||||
if (theme.resolveAttribute(attr, typedValue, true)) {
|
||||
return typedValue.getDimension(context.getResources().getDisplayMetrics());
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
private static String getAttribute(Context context, int attribute, String defaultValue) {
|
||||
TypedValue outValue = new TypedValue();
|
||||
|
||||
if (context.getTheme().resolveAttribute(attribute, outValue, true)) {
|
||||
CharSequence charSequence = outValue.coerceToString();
|
||||
if (charSequence != null) {
|
||||
return charSequence.toString();
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
54
core/ui/src/main/java/org/signal/core/ui/view/Stub.java
Normal file
54
core/ui/src/main/java/org/signal/core/ui/view/Stub.java
Normal file
@@ -0,0 +1,54 @@
|
||||
package org.signal.core.ui.view;
|
||||
|
||||
|
||||
import android.view.View;
|
||||
import android.view.ViewStub;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
||||
public class Stub<T extends View> {
|
||||
|
||||
private ViewStub viewStub;
|
||||
private T view;
|
||||
|
||||
public Stub(@NonNull ViewStub viewStub) {
|
||||
this.viewStub = viewStub;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return (viewStub != null) ? viewStub.getId() : view.getId();
|
||||
}
|
||||
|
||||
public T get() {
|
||||
if (view == null) {
|
||||
//noinspection unchecked
|
||||
view = (T) viewStub.inflate();
|
||||
viewStub = null;
|
||||
}
|
||||
|
||||
return view;
|
||||
}
|
||||
|
||||
public boolean resolved() {
|
||||
return view != null;
|
||||
}
|
||||
|
||||
public void setVisibility(int visibility) {
|
||||
if (resolved() || visibility == View.VISIBLE) {
|
||||
get().setVisibility(visibility);
|
||||
}
|
||||
}
|
||||
|
||||
public int getVisibility() {
|
||||
if (resolved()) {
|
||||
return get().getVisibility();
|
||||
} else {
|
||||
return View.GONE;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isVisible() {
|
||||
return getVisibility() == View.VISIBLE;
|
||||
}
|
||||
|
||||
}
|
||||
4
core/ui/src/main/res/values/attrs.xml
Normal file
4
core/ui/src/main/res/values/attrs.xml
Normal file
@@ -0,0 +1,4 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<attr name="theme_type" format="string"/>
|
||||
</resources>
|
||||
8
core/ui/src/main/res/values/strings.xml
Normal file
8
core/ui/src/main/res/values/strings.xml
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<resources>
|
||||
<!-- Permissions -->
|
||||
<!-- Button label to confirm and proceed with a permission request -->
|
||||
<string name="Permissions_continue">Continue</string>
|
||||
<!-- Button label to dismiss or defer a permission request -->
|
||||
<string name="Permissions_not_now">Not now</string>
|
||||
</resources>
|
||||
Reference in New Issue
Block a user