Skip to content

Commit

Permalink
Fix AppProvider service not launched (#13)
Browse files Browse the repository at this point in the history
* Implement unit tests for utils

* Fix AppProvider service not launched
  • Loading branch information
razinj authored Jan 28, 2023
1 parent d04719e commit b25725e
Show file tree
Hide file tree
Showing 18 changed files with 337 additions and 106 deletions.
10 changes: 0 additions & 10 deletions __tests__/Home-test.tsx

This file was deleted.

4 changes: 2 additions & 2 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ android {
applicationId "com.razinj.context_launcher"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 11
versionName "1.4.7"
versionCode 12
versionName "1.4.8"
archivesBaseName = "context-launcher-v$versionName-$versionCode"
}

Expand Down
12 changes: 7 additions & 5 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-permission android:name="android.permission.INTERNET" />
<!-- TODO: Find If there is a better to allow finding all apps instead of this and the ignore attribute -->
<!-- TODO: Find If there is a better way to allow finding all apps instead of this and the ignore attribute -->
<uses-permission
android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
Expand All @@ -11,6 +12,8 @@
<application
android:name=".MainApplication"
android:allowBackup="false"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="false"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
Expand All @@ -19,7 +22,6 @@
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
android:excludeFromRecents="true"
android:exported="true"
android:label="@string/app_name"
android:launchMode="singleTask"
android:windowSoftInputMode="adjustResize">
<intent-filter>
Expand All @@ -31,9 +33,9 @@
</intent-filter>
</activity>
<receiver
android:name=".PackageChangeReceiver"
android:enabled="true"
android:exported="true"
android:name=".PackageChangeReceiver">
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<action android:name="android.intent.action.PACKAGE_REPLACED" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AppProvider extends Service {

@Override
public void onCreate() {
final LauncherApps launcherApps = (LauncherApps) this.getSystemService(Context.LAUNCHER_APPS_SERVICE);
final LauncherApps launcherApps = (LauncherApps) getSystemService(Context.LAUNCHER_APPS_SERVICE);
assert launcherApps != null;

launcherApps.registerCallback(new LauncherAppsCallback() {
Expand All @@ -45,20 +45,6 @@ public void onPackageRemoved(String packageName, UserHandle user) {

PackageChangeReceiver.handleEvent(AppProvider.this, Intent.ACTION_PACKAGE_REMOVED, packageName, false);
}

@Override
public void onPackagesAvailable(String[] packageNames, UserHandle user, boolean replacing) {
if (user.equals(Process.myUserHandle())) return;

PackageChangeReceiver.handleEvent(AppProvider.this, Intent.ACTION_MEDIA_MOUNTED, null, false);
}

@Override
public void onPackagesUnavailable(String[] packageNames, UserHandle user, boolean replacing) {
if (user.equals(Process.myUserHandle())) return;

PackageChangeReceiver.handleEvent(AppProvider.this, Intent.ACTION_MEDIA_UNMOUNTED, null, false);
}
});

this.packageChangeReceiver = new PackageChangeReceiver();
Expand All @@ -67,43 +53,47 @@ public void onPackagesUnavailable(String[] packageNames, UserHandle user, boolea
appChangedIntentFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
appChangedIntentFilter.addAction(Intent.ACTION_PACKAGE_CHANGED);
appChangedIntentFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
appChangedIntentFilter.addAction(Intent.ACTION_MEDIA_MOUNTED);
appChangedIntentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
appChangedIntentFilter.addDataScheme("package");
appChangedIntentFilter.addDataScheme("file");

this.registerReceiver(packageChangeReceiver, appChangedIntentFilter);
registerReceiver(packageChangeReceiver, appChangedIntentFilter);

super.onCreate();

startForegroundCustom();
}

private void startForegroundCustom() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// Notification channel
String channelId = BuildConfig.APPLICATION_ID;
String channelName = "AppProvider Service";
NotificationChannel notificationChannel = new NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_NONE
);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

// Notification manager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);

// Notification builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.setOngoing(true)
.build();
startForeground(1, notification);
} else startForeground(2, new Notification());
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
startForeground(1, new Notification());
return;
}

// Notification channel
String channelId = BuildConfig.APPLICATION_ID;
String channelName = "AppProvider Channel";
NotificationChannel notificationChannel = new NotificationChannel(
channelId,
channelName,
NotificationManager.IMPORTANCE_NONE
);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);

// Notification manager
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert notificationManager != null;
notificationManager.createNotificationChannel(notificationChannel);

// Notification builder
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, channelId);
Notification notification = notificationBuilder
.setPriority(NotificationManager.IMPORTANCE_NONE)
.setCategory(Notification.CATEGORY_SERVICE)
.setAutoCancel(false)
.setOngoing(true)
.setSilent(true)
.build();
startForeground(1, notification);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.razinj.context_launcher;

import android.app.Application;
import android.content.Intent;
import android.os.Build;

import com.facebook.react.PackageList;
import com.facebook.react.ReactApplication;
Expand Down Expand Up @@ -57,5 +59,11 @@ public void onCreate() {
DefaultNewArchitectureEntryPoint.load();
}
ReactNativeFlipper.initializeFlipper(this, getReactNativeHost().getReactInstanceManager());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForegroundService(new Intent(this, AppProvider.class));
} else {
startService(new Intent(this, AppProvider.class));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class PackageChangeReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
String packageName = intent.getData().getSchemeSpecificPart();
Expand All @@ -34,10 +34,10 @@ public static void handleEvent(Context context, String action, String packageNam
if (launchIntent == null) return;

intent.putExtra(PACKAGE_CHANGE_IS_REMOVED, Boolean.FALSE);
} else if (!replacing) {
intent.putExtra(PACKAGE_CHANGE_IS_REMOVED, Boolean.TRUE);
} else {
} else if (replacing) {
intent.putExtra(PACKAGE_CHANGE_IS_REMOVED, Boolean.FALSE);
} else {
intent.putExtra(PACKAGE_CHANGE_IS_REMOVED, Boolean.TRUE);
}

context.sendBroadcast(intent);
Expand Down
18 changes: 18 additions & 0 deletions android/app/src/main/res/xml/data_extraction_rules.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- See details: https://developer.android.com/about/versions/12/backup-restore#xml-changes -->
<data-extraction-rules>
<cloud-backup>
<exclude domain="root" />
<exclude domain="file" />
<exclude domain="database" />
<exclude domain="sharedpref" />
<exclude domain="external" />
</cloud-backup>
<device-transfer>
<exclude domain="root" />
<exclude domain="file" />
<exclude domain="database" />
<exclude domain="sharedpref" />
<exclude domain="external" />
</device-transfer>
</data-extraction-rules>
Loading

0 comments on commit b25725e

Please sign in to comment.