I'm modifying AndroidOS to create a process that brings up a dialog when an application is launched and asks if you want to do a process kill.However, in the code I wrote, the moment I touch the application, it is forced out with an error. Here's the log and source.
I tried to implement it in ActivityThread.java. The version of AOSP is 8.1.0_r1, and the entire source code before modification is at the following link. The function on line 5429 in the following link applies.ActivityThread.java
09-11 15:55:46.797 643 1025 W WindowManager: Attempted to add application window with unknown token null. Aborting.
09-11 15:55:46.798 4971 4971 D AndroidRuntime: Shutting down VM
09-11 15:55:46.798 4971 4971 E AndroidRuntime: FATAL EXCEPTION: main
09-11 15:55:46.798 4971 4971 E AndroidRuntime: Process: (name of the application), PID: 4971
09-11 15:55:46.798 4971 4971 E AndroidRuntime: java.lang.RuntimeException: Unable to create application androidx.multidex.MultiDexApplication: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5868)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread.-wrap1(Unknown Source:0)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1738)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:106)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.os.Looper.loop(Looper.java:164)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6625)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.view.ViewRootImpl.setView(ViewRootImpl.java:765)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.Dialog.show(Dialog.java:330)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.AlertDialog$Builder.show(AlertDialog.java:1114)
09-11 15:55:46.798 4971 4971 E AndroidRuntime: at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5831)
try
{
String applicationName = ActivityThread.currentProcessName();
Log.d(TAG, "logdayonnappname:" + applicationName);
if (applicationName.contains("applications name"))
{
new AlertDialog.Builder(appContext).setTitle("title").setMessage("Hello").show();
}
mInstrumentation.callApplicationOnCreate(app);
}
catch (Exception e)
{
if (!mInstrumentation.onException(app, e))
{
throw new RuntimeException(
"Unable to create application " + app.getClass().getName()
+ ": " + e.toString(), e);
}
}
finally {
// If the app targets < O-MR1, or doesn't change the thread policy
// during startup, clobber the policy to maintain behavior of b/36951662
if (data.appInfo.targetSdkVersion <= Build.VERSION_CODES.O
|| StrictMode.getThreadPolicy().equals(writesAllowedPolicy)) {
StrictMode.setThreadPolicy(savedPolicy);
}
}
finally i could show dialog by this code but application never starts...why? I activate alert method in startActivityAsUser.
@Override
public final int startActivityAsUser(IApplicationThread caller, String callingPackage,
Intent intent, String resolvedType, IBinder resultTo, String resultWho, int requestCode,
int startFlags, ProfilerInfo profilerInfo, Bundle bOptions, int userId) {
enforceNotIsolatedCaller("startActivity");
userId = mUserController.handleIncomingUser(Binder.getCallingPid(), Binder.getCallingUid(),
userId, false, ALLOW_FULL_ONLY, "startActivity", null);
// TODO: Switch to user app stacks here.
//PackageManager mManager = getPackageManager();
// String heyyo = PackageManager.getNameForUid(userId);
String heyyo = intent.getPackage();
Log.d(TAG,"logdayonnAMS:dialooo0");
**UiHandler ui = new UiHandler();
ui.alert();**
Log.d(TAG,"logdayonnAMS:dialooo22");
String applicationName = ActivityThread.currentProcessName();
Log.d(TAG,"logdayonnAMS "+"packagebame:"+heyyo+"callingPackage:"+callingPackage+"//caller:"+caller.toString()+"//resolvedType:"+resolvedType+"//resultWho:"+resultWho);
return mActivityStarter.startActivityMayWait(caller, -1, callingPackage, intent,
resolvedType, null, null, resultTo, resultWho, requestCode, startFlags,
profilerInfo, null, null, bOptions, false, userId, null, "startActivityAsUser");
}
alert() is in the class UiHandler
public void alert() {
Log.d(TAG,"logdayonnAMS:dialooo40");
Looper.prepare();
Log.d(TAG,"logdayonnAMS:dialooo30");
Handler handler = new Handler();
new Thread(new Runnable() {
@Override
public void run() {
Log.d(TAG,"logdayonnAMS:dialooo31");
handler.post(new Runnable() {
@Override
public void run() {
AlertDialog d = new AlertDialog.Builder(mUiContext).create();
d.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
d.setCancelable(false);
d.setTitle("dialog");
d.setMessage("hey");
d.setButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Log.d(TAG,"dialog shown");
}
});
d.show();
}
});
}
}).start();
Looper.loop();
}
log is here
01-18 02:07:46.782 654 894 D ActivityManager: logdayonntest3:com.android.systemui:screenshot
01-18 02:07:46.786 1936 1936 D ActivityThread: logdayonn2android.app.ActivityThread@2b43f5a
01-18 02:07:46.805 1936 1936 I zygote64: The ClassLoaderContext is a special shared library.
01-18 02:07:46.819 1936 1936 D LoadedApk: logdayonn:com.android.systemui.SystemUIApplication
01-18 02:07:46.821 1936 1936 D LoadedApk: logdayonnappname:com.android.systemui:screenshot
01-18 02:07:46.828 1936 1936 D ActivityThread: logdayonnappstr:com.android.systemui.SystemUIApplication@a08ff67
01-18 02:07:46.828 1936 1936 D ActivityThread: logdayonnappname:com.android.systemui:screenshot
01-18 02:07:46.828 1936 1936 D ActivityThread: logdayonnAMS:dialoooa
01-18 02:07:46.828 1936 1936 D Instrumentation: logdayonnlast:com.android.systemui.SystemUIApplication@a08ff67
01-18 02:07:46.868 1936 1952 V NuMediaExtractor: setDataSource fd=33 (/system/media/audio/ui/camera_click.ogg), offset=0, length=5951
01-18 02:07:46.870 423 1132 D PermissionCache: checking android.permission.READ_FRAME_BUFFER for uid=10026 => granted (323 us)
01-18 02:07:46.873 360 360 W /system/bin/hwservicemanager: getTransport: Cannot find entry android.hardware.graphics.mapper@2.0::IMapper/default in either framework or device manifest.
01-18 02:07:46.874 1936 1936 D vndksupport: Loading /vendor/lib64/hw/android.hardware.graphics.mapper@2.0-impl.so from current namespace instead of sphal namespace.
01-18 02:07:46.875 1936 1936 D vndksupport: Loading /vendor/lib64/hw/gralloc.msm8992.so from current namespace instead of sphal namespace.
01-18 02:07:46.877 1936 1952 V NuMediaExtractor: track of type 'audio/vorbis' does not publish bitrate
01-18 02:07:46.892 548 916 I OMXMaster: makeComponentInstance(OMX.google.vorbis.decoder) in android.hardwar process
01-18 02:07:46.896 548 916 E OMXNodeInstance: getConfig(0xe9a22e40:google.vorbis.decoder, ConfigAndroidVendorExtension(0x6f100004)) ERROR: Undefined(0x80001001)
01-18 02:07:46.923 1936 1957 D OpenGLRenderer: HWUI GL Pipeline
01-18 02:07:46.982 1936 1957 I Adreno : QUALCOMM build : 8e59954, I0be83d0d26
01-18 02:07:46.982 1936 1957 I Adreno : Build Date : 09/22/17
01-18 02:07:46.982 1936 1957 I Adreno : OpenGL ES Shader Compiler Version: EV031.21.02.00
01-18 02:07:46.982 1936 1957 I Adreno : Local Branch : O17A
01-18 02:07:46.982 1936 1957 I Adreno : Remote Branch :
01-18 02:07:46.982 1936 1957 I Adreno : Remote Branch :
01-18 02:07:46.982 1936 1957 I Adreno : Reconstruct Branch :
01-18 02:07:46.986 1936 1957 D vndksupport: Loading /vendor/lib64/hw/gralloc.msm8992.so from current namespace instead of sphal namespace.
01-18 02:07:46.992 1936 1957 I Adreno : PFP: 0x00000000, ME: 0x00000000
01-18 02:07:46.997 360 360 W /system/bin/hwservicemanager: getTransport: Cannot find entry android.hardware.configstore@1.0::ISurfaceFlingerConfigs/default in either framework or device manifest.
01-18 02:07:46.998 1936 1957 I zygote64: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
01-18 02:07:47.003 1936 1957 I OpenGLRenderer: Initialized EGL, version 1.4
01-18 02:07:47.003 1936 1957 D OpenGLRenderer: Swap behavior 2
01-18 02:07:47.049 1936 1936 I AudioTrack: AUDIO_OUTPUT_FLAG_FAST successful; frameCount 20073 -> 20073
01-18 02:07:47.052 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:47.052 536 1916 D audio_route: Apply path: speaker-protected
01-18 02:07:47.057 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:47.057 536 1916 D audio_hw_primary: enable_snd_device: snd_device(95: vi-feedback)
01-18 02:07:47.057 536 1916 D audio_route: Apply path: vi-feedback
01-18 02:07:47.057 536 1916 D audio_hw_primary: enable_audio_route: usecase(24) apply and update mixer path: spkr-vi-record
01-18 02:07:47.057 536 1916 D audio_route: Apply path: spkr-vi-record
01-18 02:07:47.086 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:47.086 536 1916 D audio_hw_primary: enable_audio_route: usecase(1) apply and update mixer path: low-latency-playback
01-18 02:07:47.086 536 1916 D audio_route: Apply path: low-latency-playback
01-18 02:07:47.121 536 1916 E msm8974_platform: ramp_speaker_gain: Could not get ctl for mixer cmd - Left Speaker Gain or Right Speaker Gain, not applying speaker gain ramp
01-18 02:07:47.121 536 1916 D audio_hw_primary: out_write: retry previous failed cal level set
01-18 02:07:47.227 654 738 I ActivityManager: Waited long enough for: ServiceRecord{68285b3 u0 com.android.calendar/.alerts.InitAlarmsService}
01-18 02:07:47.297 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=31
01-18 02:07:47.510 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=26
01-18 02:07:47.983 536 605 D audio_hw_primary: disable_audio_route: usecase(1) reset and update mixer path: low-latency-playback
01-18 02:07:48.002 536 605 D audio_hw_primary: disable_snd_device: snd_device(95: vi-feedback)
01-18 02:07:48.002 536 605 D audio_hw_primary: disable_audio_route: usecase(24) reset and update mixer path: spkr-vi-record
01-18 02:07:48.298 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=32
01-18 02:07:48.462 900 900 W System : ClassLoader referenced unknown path:
01-18 02:07:48.510 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=27
01-18 02:07:49.299 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=33
01-18 02:07:49.392 654 1039 I ActivityManager: Killing 1424:com.android.provision/u0a19 (adj 906): empty #17
01-18 02:07:49.392 654 740 W zygote64: kill(-1424, 9) failed: No such process
01-18 02:07:49.396 900 900 I zygote64: Deoptimizing void com.android.systemui.statusbar.stack.NotificationStackScrollLayout.updateContentHeight() due to JIT inline cache
01-18 02:07:49.400 900 900 I zygote64: Deoptimizing void com.android.systemui.statusbar.ExpandableView.updateClipping() due to JIT inline cache
01-18 02:07:49.404 900 900 I zygote64: Deoptimizing void com.android.systemui.statusbar.stack.StackScrollAlgorithm.updateClipping(com.android.systemui.statusbar.stack.StackScrollState, com.android.systemui.statusbar.stack.StackScrollAlgorithm$StackScrollAlgorithmState, com.android.systemui.statusbar.stack.AmbientState) due to JIT inline cache
01-18 02:07:49.407 900 900 I zygote64: Deoptimizing void com.android.systemui.statusbar.stack.NotificationStackScrollLayout.updateViewShadows() due to JIT inline cache
01-18 02:07:49.423 654 894 D ActivityManager: logdayonntest2:com.android.provision
01-18 02:07:49.437 654 740 W zygote64: kill(-1424, 9) failed: No such process
01-18 02:07:49.437 654 740 I zygote64: Successfully killed process cgroup uid 10019 pid 1424 in 45ms
01-18 02:07:49.459 900 900 I zygote64: Deoptimizing void com.android.systemui.statusbar.ViewTransformationHelper.setVisible(boolean) due to JIT inline cache
01-18 02:07:49.511 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=28
01-18 02:07:50.301 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=34
01-18 02:07:50.466 1003 1656 E ImsSenderRxr: Exception in socket create'java.io.IOException: No such file or directory
01-18 02:07:50.466 1003 1656 I ImsSenderRxr: Couldn't find qmux_radio/rild_ims0socket; retrying after timeout
01-18 02:07:50.512 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=29
01-18 02:07:50.948 654 810 E QCOMPowerHAL: Failed to acquire lock.
01-18 02:07:50.949 654 810 I chatty : uid=1000(system) InputDispatcher identical 2 lines
01-18 02:07:50.949 654 810 E QCOMPowerHAL: Failed to acquire lock.
01-18 02:07:51.020 654 915 D ActivityManager: dialog shown
01-18 02:07:51.026 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:51.026 536 1916 D audio_route: Apply path: speaker-protected
01-18 02:07:51.033 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:51.033 536 1916 D audio_hw_primary: enable_snd_device: snd_device(95: vi-feedback)
01-18 02:07:51.033 536 1916 D audio_route: Apply path: vi-feedback
01-18 02:07:51.034 536 1916 D audio_hw_primary: enable_audio_route: usecase(24) apply and update mixer path: spkr-vi-record
01-18 02:07:51.034 536 1916 D audio_route: Apply path: spkr-vi-record
01-18 02:07:51.069 536 1916 E ACDB-LOADER: ACDB -> Not correctly initialized!
01-18 02:07:51.069 536 1916 D audio_hw_primary: enable_audio_route: usecase(1) apply and update mixer path: low-latency-playback
01-18 02:07:51.069 536 1916 D audio_route: Apply path: low-latency-playback
01-18 02:07:51.117 536 1916 E msm8974_platform: ramp_speaker_gain: Could not get ctl for mixer cmd - Left Speaker Gain or Right Speaker Gain, not applying speaker gain ramp
01-18 02:07:51.117 536 1916 D audio_hw_primary: out_write: retry previous failed cal level set
01-18 02:07:51.266 654 788 W AppOps : Finishing op nesting under-run: uid 1000 pkg android code 24 time=0 duration=0 nesting=0
01-18 02:07:51.301 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=35
01-18 02:07:51.514 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=30
01-18 02:07:51.631 536 605 D audio_hw_primary: disable_audio_route: usecase(1) reset and update mixer path: low-latency-playback
01-18 02:07:51.647 536 605 D audio_hw_primary: disable_snd_device: snd_device(95: vi-feedback)
01-18 02:07:51.647 536 605 D audio_hw_primary: disable_audio_route: usecase(24) reset and update mixer path: spkr-vi-record
01-18 02:07:52.303 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=36
01-18 02:07:52.516 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=31
01-18 02:07:52.574 654 810 E QCOMPowerHAL: Failed to acquire lock.
01-18 02:07:52.575 654 810 I chatty : uid=1000(system) InputDispatcher identical 2 lines
01-18 02:07:52.575 654 810 E QCOMPowerHAL: Failed to acquire lock.
01-18 02:07:53.305 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=37
01-18 02:07:53.518 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=32
01-18 02:07:54.307 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=38
01-18 02:07:54.468 1003 1656 E ImsSenderRxr: Exception in socket create'java.io.IOException: No such file or directory
01-18 02:07:54.469 1003 1656 I ImsSenderRxr: Couldn't find qmux_radio/rild_ims0socket; retrying after timeout
01-18 02:07:54.519 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=33
01-18 02:07:55.309 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=39
01-18 02:07:55.521 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=34
01-18 02:07:55.894 900 1060 I vol.Events: writeEvent level_changed STREAM_MUSIC 3
01-18 02:07:56.311 533 533 E QC-QMI : qmi_client [533]: unable to connect to server, errno=[2:No such file or directory], attempt=40
01-18 02:07:56.522 549 584 E QC-QMI : qmi_client [549]: unable to connect to server, errno=[2:No such file or directory], attempt=35
01-18 02:07:56.956 654 682 D ActivityManager: logdayonntest1:com.android.keychain
01-18 02:07:56.956 654 682 I ActivityManager: Start proc 1968:com.android.keychain/1000 for service com.android.keychain/.KeyChainService
01-18 02:07:57.001 654 932 D ActivityManager: logdayonntest3:com.android.keychain
01-18 02:07:57.008 654 932 I ActivityManager: Killing 1069:com.android.settings/1000 (adj 906): empty #17
01-18 02:07:57.012 654 932 I ActivityManager: Killing 1599:com.android.quicksearchbox/u0a57 (adj 906): empty #18
01-18 02:07:57.012 654 740 W zygote64: kill(-1069, 9) failed: No such process
01-18 02:07:57.015 1968 1968 D ActivityThread: logdayonn2android.app.ActivityThread@2b43f5a
01-18 02:07:57.037 1968 1968 I zygote64: The ClassLoaderContext is a special shared library.
01-18 02:07:57.053 1968 1968 D LoadedApk: logdayonn:null
01-18 02:07:57.055 1968 1968 D LoadedApk: logdayonnappname:com.android.keychain
01-18 02:07:57.055 1968 1968 D ActivityThread: logdayonnappstr:android.app.Application@a08ff67
01-18 02:07:57.055 1968 1968 D ActivityThread: logdayonnappname:com.android.keychain
01-18 02:07:57.055 1968 1968 D ActivityThread: logdayonnAMS:dialoooa
01-18 02:07:57.055 1968 1968 D Instrumentation: logdayonnlast:android.app.Application@a08ff67
01-18 02:07:57.057 654 740 W zygote64: kill(-1069, 9) failed: No such process
01-18 02:07:57.059 654 1406 D ActivityManager: logdayonntest2:com.android.settings
01-18 02:07:57.065 654 747 W ActivityManager: setHasOverlayUi called on unknown pid: 1069
01-18 02:07:57.065 654 682 D ActivityManager: logdayonntest2:com.android.quicksearchbox
01-18 02:07:57.083 654 682 I ActivityManager: Killing 1622:android.process.acore/u0a2 (adj 906): empty #17
01-18 02:07:57.100 654 740 W zygote64: kill(-1069, 9) failed: No such process
01-18 02:07:57.101 654 740 I zygote64: Successfully killed process cgroup uid 1000 pid 1069 in 88ms
01-18 02:07:57.101 654 740 W zygote64: kill(-1599, 9) failed: No such process
01-18 02:07:57.101 654 740 I zygote64: Successfully killed process cgroup uid 10057 pid 1599 in 0ms
01-18 02:07:57.101 654 740 W zygote64: kill(-1622, 9) failed: No such process
01-18 02:07:57.114 654 932 D ActivityManager: logdayonntest2:android.process.acore
01-18 02:07:57.147 654 740 W zygote64: kill(-1622, 9) failed: No such process
01-18 02:07:57.147 654 740 I zygote64: Successfully killed process cgroup uid 10002 pid 1622 in 45ms
The error that you are getting is "Unable to add window -- token null is not valid; is your activity running?" Which means you are trying to add a window to an activity which is not running currently. You can refer solution suggested on This Link
Also you are using appContext while creating the dialog, You need to add that particular activity's context instead of appContext.
Instead of using
if(applicationName.contains("applications name")){
new AlertDialog.Builder(appContext).setTitle("title").setMessage("Hello").show();
}
You need to use
if(applicationName.contains("applications name")){
new AlertDialog.Builder(this).setTitle("title").setMessage("Hello").show();
}
I hope this will work for you. Happy Coding :)
ActivityThread.java is running in app's process. You'd better do hook in ActivityManagerService#startActivityAsUser.
User contributions licensed under CC BY-SA 3.0