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 1 commit 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
103 changes: 102 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 Down Expand Up @@ -46,6 +50,8 @@
import androidx.annotation.NonNull;

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 +219,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 +239,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 +419,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 (isPrivateSpaceEnabled()) {
privateSpaceItem.setTitle("Disable Private Space");
} else {
privateSpaceItem.setTitle("Enable Private Space");
}
}

forwarderManager.onCreateContextMenu(menu);
}

Expand Down Expand Up @@ -557,6 +587,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 +599,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 +721,75 @@ public void onAnimationEnd(Animator animation) {
}
}

private UserHandle getPrivateUser() {
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
assert(false);
return null;
}
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 isPrivateSpaceEnabled() {
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);
}

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)

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

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

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

private void privateSpaceStateEvent(UserHandle handle) {
if (handle == null) {
return;
}
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.VANILLA_ICE_CREAM) {
assert(false);
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
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>
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -356,4 +356,5 @@
<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>
</resources>