services: Adding wm package handler
Change-Id: I1966cfcf99a563a3b603681bf1c9d5c62f69720c Signed-off-by: Ghosuto <clash.raja10@gmail.com>
This commit is contained in:
103
services/core/java/com/android/server/wm/PackageHandler.java
Normal file
103
services/core/java/com/android/server/wm/PackageHandler.java
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright (C) 2025 AxionOS Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.android.server.wm;
|
||||
|
||||
import android.content.*;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.os.Handler;
|
||||
import android.os.HandlerThread;
|
||||
import android.os.Looper;
|
||||
import android.os.UserHandle;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.android.server.UiThread;
|
||||
|
||||
class PackageHandler {
|
||||
private final Context mContext;
|
||||
private final PackageManager mPackageManager;
|
||||
private final GameListManager mGameListManager;
|
||||
private final Handler mHandler;
|
||||
|
||||
PackageHandler(Context context, GameListManager manager) {
|
||||
this.mContext = context;
|
||||
this.mPackageManager = context.getPackageManager();
|
||||
this.mGameListManager = manager;
|
||||
HandlerThread handlerThread = new HandlerThread("PackageHandler");
|
||||
handlerThread.start();
|
||||
mHandler = new Handler(handlerThread.getLooper());
|
||||
}
|
||||
|
||||
void registerPackageReceiver() {
|
||||
IntentFilter filter = new IntentFilter();
|
||||
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
|
||||
filter.addAction(Intent.ACTION_PACKAGE_FULLY_REMOVED);
|
||||
filter.addDataScheme("package");
|
||||
|
||||
mContext.registerReceiver(new BroadcastReceiver() {
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
final String pkg = intent.getData() != null ? intent.getData().getSchemeSpecificPart() : null;
|
||||
if (pkg == null) return;
|
||||
mHandler.post(() -> {
|
||||
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
|
||||
boolean isGame = isGame(pkg);
|
||||
if (isGame) {
|
||||
String label = getAppLabel(pkg);
|
||||
UiThread.getHandler().post(() -> {
|
||||
mGameListManager.addGame(pkg);
|
||||
showGameAddedNotification(label);
|
||||
});
|
||||
}
|
||||
} else if (Intent.ACTION_PACKAGE_FULLY_REMOVED.equals(intent.getAction())) {
|
||||
UiThread.getHandler().post(() -> {
|
||||
mGameListManager.removeGame(pkg);
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
}, filter);
|
||||
}
|
||||
|
||||
private boolean isGame(String pkg) {
|
||||
try {
|
||||
ApplicationInfo info = mPackageManager.getApplicationInfo(
|
||||
pkg,
|
||||
PackageManager.ApplicationInfoFlags.of(PackageManager.GET_META_DATA)
|
||||
);
|
||||
return info.category == ApplicationInfo.CATEGORY_GAME;
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private String getAppLabel(String pkg) {
|
||||
try {
|
||||
return mPackageManager
|
||||
.getApplicationLabel(
|
||||
mPackageManager.getApplicationInfo(pkg, PackageManager.ApplicationInfoFlags.of(0))
|
||||
)
|
||||
.toString();
|
||||
} catch (PackageManager.NameNotFoundException e) {
|
||||
return pkg;
|
||||
}
|
||||
}
|
||||
|
||||
private void showGameAddedNotification(String appLabel) {
|
||||
String msg = "Added " + appLabel + " to GameSpace";
|
||||
Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user