Skip to content

Commit

Permalink
wallpaper colors
Browse files Browse the repository at this point in the history
  • Loading branch information
pnemonic78 committed Sep 13, 2023
1 parent 3d95c75 commit 1646200
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 37 deletions.
6 changes: 4 additions & 2 deletions Halachic Times/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<!-- For external tones and wallpaper. -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="27" />
<!-- Foreground services for Android 9+ -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<!-- Foreground services for Android 12+ -->
<uses-permission android:name="android.permission.USE_FULL_SCREEN_INTENT" />
<!-- Notifications for Android 13+ -->
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
package com.github.times.appwidget;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.PermissionChecker;

import com.github.times.R;

import timber.log.Timber;

/**
* Shows a configuration activity for app widgets.
*
Expand All @@ -23,7 +25,10 @@
public class ZmanimWidgetConfigure extends AppCompatActivity {

private static final String PERMISSION_WALLPAPER = Manifest.permission.READ_EXTERNAL_STORAGE;
private static final int REQUEST_WALLPAPER = 0x3A11;

private final ActivityResultLauncher<String> requestPermission = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
Timber.i("Permission to read wallpaper: %s", isGranted);
});

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
Expand All @@ -36,7 +41,11 @@ protected void onCreate(@Nullable Bundle savedInstanceState) {
/**
* @see com.github.times.preference.AppearancePreferenceFragment
*/
private void checkWallpaperPermission(Context context) {
private boolean checkWallpaperPermission(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
// Wallpaper colors don't need permissions.
return true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PermissionChecker.checkCallingOrSelfPermission(context, PERMISSION_WALLPAPER) != PermissionChecker.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(this, PERMISSION_WALLPAPER)) {
Expand All @@ -45,17 +54,14 @@ private void checkWallpaperPermission(Context context) {
.setMessage(R.string.appwidget_theme_permission_rationale)
.setCancelable(true)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[]{PERMISSION_WALLPAPER}, REQUEST_WALLPAPER);
}
})
.setPositiveButton(android.R.string.ok, (dialog, which) -> requestPermission.launch(PERMISSION_WALLPAPER))
.show();
} else {
requestPermissions(new String[]{PERMISSION_WALLPAPER}, REQUEST_WALLPAPER);
requestPermission.launch(PERMISSION_WALLPAPER);
}
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
*/
package com.github.times.appwidget;

import static com.github.times.preference.ZmanimPreferences.KEY_THEME_WIDGET;
import static com.github.times.preference.ZmanimPreferences.KEY_THEME_WIDGET_RATIONALE;

import android.Manifest;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Keep;
import androidx.core.content.PermissionChecker;
import androidx.preference.ListPreference;
Expand All @@ -28,8 +33,7 @@
import com.github.times.R;
import com.github.times.preference.AbstractPreferenceFragment;

import static com.github.times.preference.ZmanimPreferences.KEY_THEME_WIDGET;
import static com.github.times.preference.ZmanimPreferences.KEY_THEME_WIDGET_RATIONALE;
import timber.log.Timber;

/**
* This fragment shows the preferences for the widgets.
Expand All @@ -38,7 +42,10 @@
public class ZmanimWidgetPreferenceFragment extends AbstractPreferenceFragment {

private static final String PERMISSION_WALLPAPER = Manifest.permission.READ_EXTERNAL_STORAGE;
private static final int REQUEST_WALLPAPER = 0x3A11;

private final ActivityResultLauncher<String> requestPermission = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
Timber.i("Permission to read wallpaper: %s", isGranted);
});

private ListPreference widgetPreference;

Expand Down Expand Up @@ -81,9 +88,13 @@ public boolean onPreferenceClick(Preference preference) {
}

private boolean checkWallpaperPermission(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
// Wallpaper colors don't need permissions.
return true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PermissionChecker.checkCallingOrSelfPermission(context, PERMISSION_WALLPAPER) != PermissionChecker.PERMISSION_GRANTED) {
requestPermissions(new String[]{PERMISSION_WALLPAPER}, REQUEST_WALLPAPER);
requestPermission.launch(PERMISSION_WALLPAPER);
return true;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,28 @@
*/
package com.github.times.preference;

import static android.content.Intent.ACTION_LOCALE_CHANGED;
import static android.text.TextUtils.isEmpty;
import static com.github.app.ActivityUtils.restartActivity;
import static com.github.preference.LocalePreferences.KEY_LOCALE;
import static com.github.times.compass.preference.CompassPreferences.KEY_THEME_COMPASS;
import static com.github.times.location.LocationPreferences.EXTRA_LOCALE;
import static com.github.times.preference.ZmanimPreferences.KEY_EMPHASIS_SCALE;
import static com.github.times.preference.ZmanimPreferences.KEY_THEME;
import static com.github.times.preference.ZmanimPreferences.KEY_THEME_WIDGET;
import static com.github.times.preference.ZmanimPreferences.KEY_THEME_WIDGET_RATIONALE;
import static com.github.util.LocaleUtils.sortByDisplay;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.Keep;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
Expand All @@ -38,17 +51,7 @@

import java.util.Locale;

import static android.content.Intent.ACTION_LOCALE_CHANGED;
import static android.text.TextUtils.isEmpty;
import static com.github.app.ActivityUtils.restartActivity;
import static com.github.preference.LocalePreferences.KEY_LOCALE;
import static com.github.times.compass.preference.CompassPreferences.KEY_THEME_COMPASS;
import static com.github.times.location.LocationPreferences.EXTRA_LOCALE;
import static com.github.times.preference.ZmanimPreferences.KEY_EMPHASIS_SCALE;
import static com.github.times.preference.ZmanimPreferences.KEY_THEME;
import static com.github.times.preference.ZmanimPreferences.KEY_THEME_WIDGET;
import static com.github.times.preference.ZmanimPreferences.KEY_THEME_WIDGET_RATIONALE;
import static com.github.util.LocaleUtils.sortByDisplay;
import timber.log.Timber;

/**
* This fragment shows the preferences for the Appearance header.
Expand All @@ -57,10 +60,13 @@
public class AppearancePreferenceFragment extends AbstractPreferenceFragment {

private static final String PERMISSION_WALLPAPER = Manifest.permission.READ_EXTERNAL_STORAGE;
private static final int REQUEST_WALLPAPER = 0x3A11;

private ListPreference widgetPreference;

private final ActivityResultLauncher<String> requestPermission = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
Timber.i("Permission to read wallpaper: %s", isGranted);
});

@Override
protected int getPreferencesXml() {
return R.xml.appearance_preferences;
Expand Down Expand Up @@ -158,6 +164,10 @@ public boolean onPreferenceClick(Preference preference) {
}

private boolean checkWallpaperPermission(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O_MR1) {
// Wallpaper colors don't need permissions.
return true;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (PermissionChecker.checkCallingOrSelfPermission(context, PERMISSION_WALLPAPER) != PermissionChecker.PERMISSION_GRANTED) {
final Activity activity = getActivity();
Expand All @@ -167,15 +177,10 @@ private boolean checkWallpaperPermission(Context context) {
.setMessage(R.string.appwidget_theme_permission_rationale)
.setCancelable(true)
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[]{PERMISSION_WALLPAPER}, REQUEST_WALLPAPER);
}
})
.setPositiveButton(android.R.string.ok, (dialog, which) -> requestPermission.launch(PERMISSION_WALLPAPER))
.show();
} else {
requestPermissions(new String[]{PERMISSION_WALLPAPER}, REQUEST_WALLPAPER);
requestPermission.launch(PERMISSION_WALLPAPER);
}
return true;
}
Expand Down

0 comments on commit 1646200

Please sign in to comment.