Skip to content

Commit

Permalink
Add a preference dialog and a make-default option
Browse files Browse the repository at this point in the history
Move the orientation toggle into a preference dialog and also
add an option to set the launcher as default.
  • Loading branch information
markusfisch committed Jun 29, 2023
1 parent 135f6b1 commit 62aac5c
Show file tree
Hide file tree
Showing 13 changed files with 200 additions and 43 deletions.
3 changes: 1 addition & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
android:name=".app.PieLauncherApp"
android:allowBackup="true"
android:supportsRtl="true"
android:theme="@style/HomeTheme"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher">
<activity
Expand All @@ -26,7 +27,6 @@
android:taskAffinity=""
android:excludeFromRecents="true"
android:exported="true"
android:theme="@style/HomeTheme"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
Expand All @@ -47,7 +47,6 @@
</activity>
<activity
android:name=".activity.BatteryOptimizationActivity"
android:theme="@style/HomeTheme"
android:exported="true"/>
</application>
</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
Expand Down Expand Up @@ -60,13 +58,7 @@ public boolean dispatchTouchEvent(MotionEvent ev) {
protected void onCreate(Bundle state) {
super.onCreate(state);

setRequestedOrientation(PieLauncherApp.prefs.getOrientation(
// Default to current orientation, whatever that is.
// This should naturally be landscape for tablets and
// portrait for phones.
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2
? ActivityInfo.SCREEN_ORIENTATION_LOCKED
: ActivityInfo.SCREEN_ORIENTATION_NOSENSOR));
setRequestedOrientation(PieLauncherApp.prefs.getOrientation());

kb = new SoftKeyboard(this);
gestureDetector = new GestureDetector(this, new FlingListener(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package de.markusfisch.android.pielauncher.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.role.RoleManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import de.markusfisch.android.pielauncher.R;
import de.markusfisch.android.pielauncher.app.PieLauncherApp;

public class PreferencesDialog {
public static void create(Context context) {
Activity activity = (Activity) context;
LayoutInflater inflater = activity.getLayoutInflater();
View view = inflater.inflate(R.layout.dialog_preferences, null);

TextView orientationView = view.findViewById(R.id.orientation);
orientationView.setOnClickListener(
v -> setOrientation(activity, orientationView));
setOrientationText(orientationView,
PieLauncherApp.prefs.getOrientation());

TextView defaultLauncherView = view.findViewById(
R.id.make_default_launcher);

AlertDialog dialog = new AlertDialog.Builder(context)
.setTitle(R.string.preferences)
.setView(view)
.setPositiveButton(android.R.string.ok, null)
.show();

if (isDefault(
activity.getPackageManager(),
activity.getPackageName())) {
defaultLauncherView.setVisibility(View.GONE);
} else {
defaultLauncherView.setOnClickListener(v -> {
setAsDefault(activity);
dialog.dismiss();
});
}
}

private static void setOrientation(Activity activity,
TextView orientationView) {
int newOrientation = PieLauncherApp.prefs.getOrientation() ==
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
: ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
PieLauncherApp.prefs.setOrientation(newOrientation);
setOrientationText(orientationView, newOrientation);
activity.setRequestedOrientation(newOrientation);
}

private static void setOrientationText(TextView tv, int orientation) {
tv.setText(getOrientationResId(orientation));
}

private static int getOrientationResId(int orientation) {
switch (orientation) {
default:
return R.string.orientation_default;
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
return R.string.orientation_landscape;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
return R.string.orientation_portrait;
}
}

private static void setAsDefault(Activity activity) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
Intent intent = new Intent("android.intent.action.MAIN");
intent.addCategory("android.intent.category.HOME");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
} else {
RoleManager roleManager = (RoleManager) activity.getSystemService(
Context.ROLE_SERVICE);
if (roleManager.isRoleAvailable(RoleManager.ROLE_HOME) &&
!roleManager.isRoleHeld(RoleManager.ROLE_HOME)) {
activity.startActivityForResult(
roleManager.createRequestRoleIntent(
RoleManager.ROLE_HOME),
1);
}
}
}

private static boolean isDefault(PackageManager packageManager,
String packageName) {
IntentFilter filter = new IntentFilter(Intent.ACTION_MAIN);
filter.addCategory(Intent.CATEGORY_HOME);

List<IntentFilter> filters = new ArrayList<>();
filters.add(filter);

List<ComponentName> activities = new ArrayList<>();
packageManager.getPreferredActivities(filters, activities, null);

for (ComponentName activity : activities) {
if (packageName.equals(activity.getPackageName())) {
return true;
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,52 @@

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.preference.PreferenceManager;
import android.util.DisplayMetrics;

public class Preferences {
private static final String ORIENTATION = "orientation";
private static final String RADIUS = "radius";
private static final String ORIENTATION = "orientation";

private SharedPreferences preferences;
private int defaultOrientation;

public void init(Context context) {
preferences = PreferenceManager.getDefaultSharedPreferences(context);
DisplayMetrics dm = context.getResources().getDisplayMetrics();
defaultOrientation = dm.heightPixels > dm.widthPixels
? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
: ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
}

public int getOrientation(int preset) {
return preferences.getInt(ORIENTATION, preset);
public int getOrientation() {
return preferences.getInt(ORIENTATION, defaultOrientation);
}

public void setOrientation(int orientation) {
apply(ORIENTATION, orientation);
put(ORIENTATION, orientation).commit();
}

public int getRadius(int preset) {
return preferences.getInt(RADIUS, preset);
}

public void setRadius(int radius) {
apply(RADIUS, radius);
put(RADIUS, radius).apply();
}

private void apply(String key, int value) {
put(key, value).apply();
private SharedPreferences.Editor put(String key, int value) {
return put(editor -> editor.putInt(key, value));
}

private SharedPreferences.Editor put(String key, int value) {
private SharedPreferences.Editor put(PutListener listener) {
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(key, value);
listener.put(editor);
return editor;
}

private interface PutListener {
void put(SharedPreferences.Editor editor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
Expand Down Expand Up @@ -34,6 +33,7 @@
import de.markusfisch.android.pielauncher.R;
import de.markusfisch.android.pielauncher.app.PieLauncherApp;
import de.markusfisch.android.pielauncher.content.AppMenu;
import de.markusfisch.android.pielauncher.dialog.PreferencesDialog;
import de.markusfisch.android.pielauncher.graphics.CanvasPieMenu;
import de.markusfisch.android.pielauncher.graphics.Converter;
import de.markusfisch.android.pielauncher.graphics.Ripple;
Expand Down Expand Up @@ -78,8 +78,8 @@ public interface ListListener {
private final Bitmap iconInfo;
private final Rect iconDoneRect = new Rect();
private final Bitmap iconDone;
private final Rect iconRotateRect = new Rect();
private final Bitmap iconRotate;
private final Rect iconPreferencesRect = new Rect();
private final Bitmap iconPreferences;
private final Bitmap iconLaunchFirst;
private final String numberOfIconsTip;
private final String dragToOrderTip;
Expand Down Expand Up @@ -148,7 +148,7 @@ public AppPieView(Context context, AttributeSet attr) {
iconRemove = getBitmapFromDrawable(res, R.drawable.ic_remove);
iconInfo = getBitmapFromDrawable(res, R.drawable.ic_info);
iconDone = getBitmapFromDrawable(res, R.drawable.ic_done);
iconRotate = getBitmapFromDrawable(res, R.drawable.ic_rotation);
iconPreferences = getBitmapFromDrawable(res, R.drawable.ic_preferences);
iconLaunchFirst = getBitmapFromDrawable(res,
R.drawable.ic_launch_first);
iconLaunchFirstHalf = iconLaunchFirst.getWidth() >> 1;
Expand Down Expand Up @@ -577,8 +577,8 @@ private void initMenu(int width, int height) {
}

private void layoutEditorControls(boolean portrait) {
Bitmap[] icons = new Bitmap[]{iconAdd, iconRemove, iconRotate, iconInfo, iconDone};
Rect[] rects = new Rect[]{iconAddRect, iconRemoveRect, iconRotateRect, iconInfoRect,
Bitmap[] icons = new Bitmap[]{iconAdd, iconRemove, iconPreferences, iconInfo, iconDone};
Rect[] rects = new Rect[]{iconAddRect, iconRemoveRect, iconPreferencesRect, iconInfoRect,
iconDoneRect};
int length = icons.length;
int totalWidth = 0;
Expand Down Expand Up @@ -742,16 +742,8 @@ private boolean performEditAction(Context context) {
endEditMode();
}
successful = true;
} else if (iconRotateRect.contains(touch.x, touch.y)) {
int newOrientation = viewHeight > viewWidth
? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
: ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
PieLauncherApp.prefs.setOrientation(newOrientation);
// Make an attempt to apply new orientation if possible.
Activity activity = (Activity) getContext();
if (activity != null) {
activity.setRequestedOrientation(newOrientation);
}
} else if (iconPreferencesRect.contains(touch.x, touch.y)) {
PreferencesDialog.create(context);
successful = true;
}
grabbedIcon = null;
Expand Down Expand Up @@ -873,7 +865,7 @@ private void drawEditor(Canvas canvas) {
drawTip(canvas, getTip(hasIcon));
drawIcon(canvas, iconAdd, iconAddRect, !hasIcon);
drawIcon(canvas, iconRemove, iconRemoveRect, hasIcon);
drawIcon(canvas, iconRotate, iconRotateRect, !hasIcon);
drawIcon(canvas, iconPreferences, iconPreferencesRect, !hasIcon);
drawIcon(canvas, iconInfo, iconInfoRect, hasIcon);
drawIcon(canvas, iconDone, iconDoneRect, !hasIcon);
int centerX = viewWidth >> 1;
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/drawable/ic_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:width="64dp" android:height="64dp" android:viewportWidth="64" android:viewportHeight="64">
<path android:pathData="M39.14 32.94c0.04-0.3 0.06-0.61 0.06-0.94 0-0.32-0.02-0.64-0.07-0.94l2.03-1.58c0.18-0.14 0.23-0.41 0.12-0.61l-1.92-3.32c-0.12-0.22-0.37-0.29-0.59-0.22l-2.39 0.96c-0.5-0.38-1.03-0.7-1.62-0.94l-0.36-2.54c-0.04-0.24-0.24-0.41-0.48-0.41h-3.84c-0.24 0-0.43 0.17-0.47 0.41l-0.36 2.54c-0.59 0.24-1.13 0.57-1.62 0.94l-2.39-0.96c-0.22-0.08-0.47 0-0.59 0.22l-1.91 3.32c-0.12 0.21-0.08 0.47 0.12 0.61l2.03 1.58c-0.05 0.3-0.09 0.63-0.09 0.94 0 0.31 0.02 0.64 0.07 0.94l-2.03 1.58c-0.18 0.14-0.23 0.41-0.12 0.61l1.92 3.32c0.12 0.22 0.37 0.29 0.59 0.22l2.39-0.96c0.5 0.38 1.03 0.7 1.62 0.94l0.36 2.54c0.05 0.24 0.24 0.41 0.48 0.41h3.84c0.24 0 0.44-0.17 0.47-0.41l0.36-2.54c0.59-0.24 1.13-0.56 1.62-0.94l2.39 0.96c0.22 0.08 0.47 0 0.59-0.22l1.92-3.32c0.12-0.22 0.07-0.47-0.12-0.61l-2.01-1.58zM32 35.6c-1.98 0-3.6-1.62-3.6-3.6s1.62-3.6 3.6-3.6 3.6 1.62 3.6 3.6-1.62 3.6-3.6 3.6z" android:fillColor="#FFFFFF"/>
</vector>
5 changes: 0 additions & 5 deletions app/src/main/res/drawable/ic_rotation.xml

This file was deleted.

20 changes: 20 additions & 0 deletions app/src/main/res/layout/dialog_preferences.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">
<TextView
style="@style/Preference"
android:id="@+id/orientation"
android:text="@string/orientation_default"/>
<TextView
style="@style/Preference"
android:id="@+id/make_default_launcher"
android:text="@string/make_default_launcher"/>
</LinearLayout>
</ScrollView>
6 changes: 5 additions & 1 deletion app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Pie Launcher</string>
<string name="tip_number_of_icons">Essayer 4, 6 ou 8 symboles pour une utilisation optimale</string>
<string name="tip_pinch_zoom">Pincer et écarter pour redimensionner le menu</string>
<string name="tip_drag_to_order">Faire glisser pour organiser</string>
<string name="please_ignore_battery_optimization">Merci de désactiver les optimisations de batterie pour Pie Launcher afin d\'économiser de l\'énergie et le faire fonctionner plus rapidement.\n\nPie Launcher ne consomme pas de batterie quand il n\'est pas utilisé. Un lanceur d\'application est une exception parce qu\'il devrait être disponible en permanence et immédiatement, ce qui est empêché par les optimisations de batterie.\n\nPour cette raison, le lanceur d\'application par défaut est aussi déjà exclu des optimisations de batterie.</string>
<string name="disable_battery_optimization">DÉSACTIVER LES OPTIMISATIONS DE BATTERIE</string>
<string name="preferences">Préférences</string>
<string name="orientation_default"><big>Orientation</big>\nDefault</string>
<string name="orientation_portrait"><big>Orientation</big>\nPortrait</string>
<string name="orientation_landscape"><big>Orientation</big>\nPaysage</string>
<string name="make_default_launcher"><big>Créer un lanceur par défaut</big>\nDéfinir comme lanceur par défaut</string>
</resources>
8 changes: 8 additions & 0 deletions app/src/main/res/values-zh-rCN/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@
<string name="tip_drag_to_order">拖动以排列顺序</string>
<string name="please_ignore_battery_optimization">请 对 Pie 启动器禁用电池优化,以节省电量,并使其工作更快。\n\nPie 启动器不使用时不消耗电池。一个主屏幕启动程序是一个例外,因为它应该总是和立即可用,而这是由电池优化所阻止的。\n\n因此,默认启动器也被排除在电池优化之外。</string>
<string name="disable_battery_optimization">禁用电池优化</string>
<string name="preferences">偏好</string>
<string name="orientation_default"><big>
方向</big>\n默認</string>
<string name="orientation_portrait"><big>
方向</big>\n縱向</string>
<string name="orientation_landscape"><big>
方向</big>\n橫向</string>
<string name="make_default_launcher"><big>設為默認啟動器</big>\n設置為默認啟動器。</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@
<string name="tip_drag_to_order">Drag to arrange order</string>
<string name="please_ignore_battery_optimization">Please disable battery optimizations for Pie Launcher to save energy and make it work faster.\n\nPie Launcher does not consume battery when not in use. A home screen launcher is an exception because it should always and immediately be available, which is prevented by battery optimizations.\n\nFor this reason, the default launcher is already excluded from battery optimizations too.</string>
<string name="disable_battery_optimization">DISABLE BATTERY OPTIMIZATION</string>
<string name="preferences">Preferences</string>
<string name="orientation_default"><big>Orientation</big>\nDefault</string>
<string name="orientation_portrait"><big>Orientation</big>\nPortrait</string>
<string name="orientation_landscape"><big>Orientation</big>\nLandscape</string>
<string name="make_default_launcher"><big>Make default launcher</big>\nSet as default launcher</string>
</resources>
5 changes: 5 additions & 0 deletions app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,9 @@
<item name="android:textColor">@color/text_color</item>
<item name="android:textStyle">bold</item>
</style>
<style name="Preference" parent="@android:style/Widget.TextView">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginBottom">16dp</item>
</style>
</resources>
4 changes: 4 additions & 0 deletions svg/ic_preferences.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 62aac5c

Please sign in to comment.