Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android 15 Private Space Integration #2345

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ plugins {

android {
defaultConfig {
compileSdk 34
compileSdk 35
}
buildFeatures {
buildConfig = true
}
defaultConfig {
applicationId 'fr.neamar.kiss'
minSdkVersion 15
targetSdkVersion 34
targetSdkVersion 35
versionCode 211
versionName "3.21.3"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
-->
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
<!-- To provide access and state handling of the private space -->
<uses-permission android:name="android.permission.ACCESS_HIDDEN_PROFILES" />

<uses-feature
android:name="android.hardware.telephony"
Expand Down Expand Up @@ -66,6 +68,7 @@
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.APP_MARKET" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.ASSIST" />
Expand Down
93 changes: 92 additions & 1 deletion app/src/main/java/fr/neamar/kiss/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.LauncherApps;
import android.content.pm.LauncherUserInfo;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
Expand All @@ -19,6 +21,8 @@
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.os.UserHandle;
import android.os.UserManager;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.text.Editable;
Expand All @@ -44,8 +48,11 @@
import android.widget.TextView.OnEditorActionListener;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;

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

import fr.neamar.kiss.adapter.RecordAdapter;
import fr.neamar.kiss.broadcast.IncomingCallHandler;
Expand Down Expand Up @@ -213,6 +220,11 @@ public void onReceive(Context context, Intent intent) {

// Run GC once to free all the garbage accumulated during provider initialization
System.gc();
} else if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_PROFILE_AVAILABLE)
|| intent.getAction().equalsIgnoreCase(Intent.ACTION_PROFILE_UNAVAILABLE)) {
privateSpaceStateEvent(intent.getParcelableExtra(Intent.EXTRA_USER, UserHandle.class));
}
}

// New provider might mean new favorites
Expand All @@ -228,6 +240,12 @@ public void onReceive(Context context, Intent intent) {
this.registerReceiver(mReceiver, intentFilterLoad, Context.RECEIVER_EXPORTED);
this.registerReceiver(mReceiver, intentFilterLoadOver, Context.RECEIVER_EXPORTED);
this.registerReceiver(mReceiver, intentFilterFullLoadOver, Context.RECEIVER_EXPORTED);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
IntentFilter intentFilterProfileAvailable = new IntentFilter(Intent.ACTION_PROFILE_AVAILABLE);
IntentFilter intentFilterProfileUnAvailable = new IntentFilter(Intent.ACTION_PROFILE_UNAVAILABLE);
this.registerReceiver(mReceiver, intentFilterProfileAvailable, Context.RECEIVER_EXPORTED);
this.registerReceiver(mReceiver, intentFilterProfileUnAvailable, Context.RECEIVER_EXPORTED);
}
}
else {
this.registerReceiver(mReceiver, intentFilterLoad);
Expand Down Expand Up @@ -402,6 +420,19 @@ public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMen
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);

MenuItem privateSpaceItem = menu.findItem(R.id.private_space);
if (privateSpaceItem != null) {
if ((android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM)
|| (getPrivateUser() == null)) {
privateSpaceItem.setVisible(false);
} else if (isPrivateSpaceUnlocked()) {
privateSpaceItem.setTitle("Lock Private Space");
} else {
privateSpaceItem.setTitle("Unlock Private Space");
}
}

forwarderManager.onCreateContextMenu(menu);
}

Expand Down Expand Up @@ -557,6 +588,9 @@ public boolean onOptionsItemSelected(MenuItem item) {
} else if (itemId == R.id.preferences) {
startActivity(new Intent(this, SettingsActivity.class));
return true;
} else if (itemId == R.id.private_space) {
switchPrivateSpaceState();
return true;
}
return super.onOptionsItemSelected(item);
}
Expand All @@ -566,7 +600,6 @@ public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);

return true;
}

Expand Down Expand Up @@ -689,6 +722,64 @@ public void onAnimationEnd(Animator animation) {
}
}

@RequiresApi(35)
private UserHandle getPrivateUser() {
final UserManager manager = (UserManager) this.getSystemService(Context.USER_SERVICE);
assert manager != null;

final LauncherApps launcher = (LauncherApps) this.getSystemService(Context.LAUNCHER_APPS_SERVICE);
assert launcher != null;

List<UserHandle> users = launcher.getProfiles();

UserHandle privateUser = null;
for (UserHandle user : users) {
if (Objects.requireNonNull(launcher.getLauncherUserInfo(user)).getUserType().equalsIgnoreCase(UserManager.USER_TYPE_PROFILE_PRIVATE)) {
privateUser = user;
break;
}
}
return privateUser;
}

private boolean isPrivateSpaceUnlocked() {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
return false;
}

final UserManager manager = (UserManager) this.getSystemService(Context.USER_SERVICE);
assert manager != null;

UserHandle user = getPrivateUser();
return !manager.isQuietModeEnabled(user);
}

@RequiresApi(35)
private void switchPrivateSpaceState() {
Copy link
Collaborator

@TBog TBog Nov 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could annotate the method switchPrivateSpaceState instead of asserting inside
@RequiresApi(35)

final UserManager manager = (UserManager) this.getSystemService(Context.USER_SERVICE);
assert manager != null;

UserHandle user = getPrivateUser();
manager.requestQuietModeEnabled(!manager.isQuietModeEnabled(user), user);
}

@RequiresApi(35)
private void privateSpaceStateEvent(UserHandle handle) {
if (handle == null) {
return;
}

final LauncherApps launcher = (LauncherApps) this.getSystemService(Context.LAUNCHER_APPS_SERVICE);

LauncherUserInfo info = launcher.getLauncherUserInfo(handle);
if (info != null) {
if (info.getUserType().equalsIgnoreCase(UserManager.USER_TYPE_PROFILE_PRIVATE)) {
Log.d(TAG, "Private Space state changed");
// TODO: Check if private space state changed and change app view accordingly
}
}
}

public void onFavoriteChange() {
forwarderManager.onFavoriteChange();
}
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/fr/neamar/kiss/SettingsActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,8 @@ public void onDenied() {
}
} else if ("selected-contact-mime-types".equals(key)) {
getDataHandler().reloadContactsProvider();
} else if ("disable-private-space-shortcuts".equals(key)) {
getDataHandler().reloadShortcuts();
}
}

Expand Down
18 changes: 17 additions & 1 deletion app/src/main/java/fr/neamar/kiss/loader/LoadAppPojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.content.pm.ApplicationInfo;
import android.content.pm.LauncherActivityInfo;
import android.content.pm.LauncherApps;
import android.content.pm.LauncherUserInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Build;
Expand Down Expand Up @@ -55,6 +56,10 @@ protected List<AppPojo> doInBackground(Void... params) {

// Handle multi-profile support introduced in Android 5 (#542)
for (android.os.UserHandle profile : manager.getUserProfiles()) {
LauncherUserInfo info = null;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
info = launcherApps.getLauncherUserInfo(profile);
}
UserHandle user = new UserHandle(manager.getSerialNumberForUser(profile), profile);
for (LauncherActivityInfo activityInfo : launcherApps.getActivityList(null, profile)) {
if (isCancelled()) {
Expand All @@ -63,7 +68,18 @@ protected List<AppPojo> doInBackground(Void... params) {
ApplicationInfo appInfo = activityInfo.getApplicationInfo();
boolean disabled = PackageManagerUtils.isAppSuspended(appInfo) || isQuietModeEnabled(manager, profile);
final AppPojo app = createPojo(user, appInfo.packageName, activityInfo.getName(), activityInfo.getLabel(), disabled, excludedAppList, excludedFromHistoryAppList, excludedShortcutsAppList);
apps.add(app);
if ((android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM)
&& (info != null)) {
if (!info.getUserType().equalsIgnoreCase(UserManager.USER_TYPE_PROFILE_PRIVATE)) {
apps.add(app);
} else {
if (!isQuietModeEnabled(manager, profile)) {
apps.add(app);
}
}
} else {
apps.add(app);
}
}
}
} else {
Expand Down
44 changes: 43 additions & 1 deletion app/src/main/java/fr/neamar/kiss/loader/LoadShortcutsPojos.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@
package fr.neamar.kiss.loader;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.pm.LauncherApps;
import android.content.pm.LauncherUserInfo;
import android.content.pm.ShortcutInfo;
import android.os.Build;
import android.os.UserManager;
import android.preference.PreferenceManager;

import androidx.annotation.RequiresApi;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -33,6 +39,8 @@ protected List<ShortcutPojo> doInBackground(Void... params) {
return new ArrayList<>();
}

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);

List<ShortcutRecord> records = DBHelper.getShortcuts(context);
DataHandler dataHandler = KissApplication.getApplication(context).getDataHandler();
TagsHandler tagsHandler = dataHandler.getTagsHandler();
Expand All @@ -52,8 +60,13 @@ protected List<ShortcutPojo> doInBackground(Void... params) {
// get all oreo shortcuts from system directly
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
UserManager userManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
LauncherApps launcherApps = (LauncherApps) context.getSystemService(Context.LAUNCHER_APPS_SERVICE);
List<ShortcutInfo> shortcutInfos = ShortcutUtil.getAllShortcuts(context);
for (ShortcutInfo shortcutInfo : shortcutInfos) {
LauncherUserInfo info = null;
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM) {
info = launcherApps.getLauncherUserInfo(shortcutInfo.getUserHandle());
}
if (isCancelled()) {
break;
}
Expand All @@ -62,7 +75,16 @@ protected List<ShortcutPojo> doInBackground(Void... params) {
if (shortcutRecord != null) {
boolean disabled = PackageManagerUtils.isAppSuspended(context, shortcutInfo.getPackage(), new UserHandle(context, shortcutInfo.getUserHandle())) || userManager.isQuietModeEnabled(shortcutInfo.getUserHandle());
ShortcutPojo pojo = createPojo(shortcutRecord, tagsHandler, ShortcutUtil.getComponentName(context, shortcutInfo), shortcutInfo.isPinned(), shortcutInfo.isDynamic(), disabled);
pojos.add(pojo);
if ((android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.VANILLA_ICE_CREAM)
&& (info != null)) {
boolean privateSpaceShortcutsDisabled = prefs.getBoolean("disable-private-space-shortcuts", true);
if (shouldAddShortcut(userManager, info, shortcutInfo.getUserHandle(),
privateSpaceShortcutsDisabled)) {
pojos.add(pojo);
}
} else {
pojos.add(pojo);
}
}
}
}
Expand All @@ -71,10 +93,30 @@ protected List<ShortcutPojo> doInBackground(Void... params) {
return pojos;
}

@RequiresApi(35)
private boolean shouldAddShortcut(UserManager manager, LauncherUserInfo info,
android.os.UserHandle profile, boolean privateSpaceShortcutsDisabled) {
if (!info.getUserType().equalsIgnoreCase(UserManager.USER_TYPE_PROFILE_PRIVATE)) {
return true;
} else {
if (privateSpaceShortcutsDisabled) {
return false;
}
else return !isQuietModeEnabled(manager, profile);
}
}

private ShortcutPojo createPojo(ShortcutRecord shortcutRecord, TagsHandler tagsHandler, String componentName, boolean pinned, boolean dynamic, boolean disabled) {
ShortcutPojo pojo = new ShortcutPojo(shortcutRecord, componentName, pinned, dynamic, disabled);
pojo.setName(shortcutRecord.name);
pojo.setTags(tagsHandler.getTags(pojo.id));
return pojo;
}

private boolean isQuietModeEnabled(UserManager manager, android.os.UserHandle profile) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return manager.isQuietModeEnabled(profile);
}
return false;
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/menu/menu_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@
android:id="@+id/settings"
android:showAsAction="never"
android:title="@string/menu_settings" />
<item
android:id="@+id/private_space"
android:showAsAction="never"
android:title="@string/menu_private_space" />

</menu>
15 changes: 15 additions & 0 deletions app/src/main/res/values-v35/themes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!-- TODO: Remove once activities handle insets. -->
<style name="BaseThemeLight" parent="@android:style/Theme.Holo.Light.NoActionBar">
<item name="appSelectableItemBackground">?android:attr/selectableItemBackground</item>
<item name="android:actionMenuTextColor">@android:color/white</item>
<item name="android:windowOptOutEdgeToEdgeEnforcement">true</item>
</style>

<style name="BaseThemeDark" parent="@android:style/Theme.Holo.NoActionBar">
<item name="appSelectableItemBackground">?android:attr/selectableItemBackground</item>
<item name="android:windowOptOutEdgeToEdgeEnforcement">true</item>
</style>
</resources>
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,6 @@
<string name="tagged_result_sort_mode_name">Tagged result sort mode</string>
<string name="tagged_result_sort_mode_desc">Select how results should be sorted when shown by tag</string>
<string name="tags_category">Tags</string>
<string name="menu_private_space">Private Space</string>
<string name="disable_private_space_shortcuts">Disable Private Space app shortcuts</string>
</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@
android:defaultValue="false"
android:key="call-contact-on-click"
android:title="@string/contacts_call_on_click" />
<fr.neamar.kiss.preference.SwitchPreference
android:defaultValue="true"
android:key="disable-private-space-shortcuts"
android:title="@string/disable_private_space_shortcuts" />
</PreferenceCategory>
<PreferenceScreen
android:key="wallpaper-holder"
Expand Down