How to disconnect from Cast when finishing Activity? - android

Here is what I'm doing:
Manually connecting to ChromeCast via Settings.
Launching my app which manages Presentation to show custom layout using the ChromeCast device I'm already connected to.
Finishing my app by manually closing it.
By this time, I can still see my device screen casting. Here is where I want to disconnect from casting programmatically so I cannot longer see the screen casting.
While doing the presentation I have access to the Display I'm casting to:
MediaRouter.RouteInfo route = mMediaRouter.getSelectedRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO);
return route != null ? route.getPresentationDisplay() : null;
Any ideas on how to achive this functionality?
UPDATE:
For those who are interested on this, this is how I did it (Thanks to Ali Naddaf response):
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
public void disconnect(){
MediaRouter mMediaRouter = (MediaRouter) getSystemService(Context.MEDIA_ROUTER_SERVICE);
mMediaRouter.selectRoute(MediaRouter.ROUTE_TYPE_LIVE_VIDEO, mMediaRouter.getDefaultRoute());
}

For the ones like me who came looking for the answer using v3 Cast SDK:
mSessionManager.endCurrentSession(true);

Find SessionManager as below and disconnect from chromecast session:
mRemoteMediaClient.stop(); // stop remote media
CastContext castContext = CastContext.getSharedInstance(this);
SessionManager mSessionManager = castContext.getSessionManager();
mSessionManager.endCurrentSession(true);

If I understand correctly, you are having user start a screen casting/mirroring outside of your app. Then user enters your app and when they leave, you want to disconnect? If I am not correct, please let me know.
It is possible to disconnect the screen mirroring but I am not sure it is the right thing to do. When user enters your app, you do not know if the user had turned on the screen casting because of your app, or some other reason. If the user had done that for some other reason prior to entering your app, it wouldn't be right for your app to disconnect the user upon leaving. Would you agree?

This worked for me:
mediarouter.unselect(0)

Related

App open event type in Accessibility Service

I'm working on Accessibility Service, but I'm not getting events of app start. I am using TYPE_VIEW_FOCUSED currently. Please let me know how can I get events of when any app starts.
info.eventTypes = AccessibilityEvent.TYPE_VIEW_FOCUSED;
You can try using AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED .. It will trigger when top application has changed.
fun onAccessibilityEvent(event: AccessibilityEvent) {
if (event.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
event.packageName // Top application package name
}
}
Disclaimer - I never tried this code. I was reading about AccessibilityService since last few days. So let me know if it works or not .. i'll gladly remove this answer if its not correct. have a look at This Sample..
ADM's code indeed works - but just be aware that you may get more than one TYPE_WINDOW_STATE_CHANGED event for each app.
For example, when I launch Chrome, my app receives two events within milliseconds of each other. The first is for "ClassName: android.widget.FrameLayout" and the second for "ClassName: org.chromium.chrome.browser.ChromeTabbedActivity".
When I launch YouTube, my app receives four events in rapid succession: "ClassName: android.widget.FrameLayout", "ClassName: android.widget.ImageView", "ClassName: android.widget.FrameLayout", "ClassName: com.google.android.apps.youtube.app.watchwhile.WatchWhileActivity".
In either case, only one event has "FullScreen: true", so maybe you can filter on that.

Disabling first-run greeter on fresh android emulator

I am writing a test that requires launching application directly from launcher. Because I can't emulate it correctly by launching through intent.
The problem is that when I am running the test on a fresh emulator (I am using Travis CI, but it can be easily reproduced on my home PC) the emulator starts with the "first run" greeter overlay. Which blocks my uiautomator code from correctly launching the application.
I have tried to add some code to close that greeter but unfortunately it can appear with some delay, when my "greeter detecting and closing" code has already stopped working thinking that the coast is clear.
Is there any guaranteed way to disable that greeter? Some preference maybe or just an example of code that will reliably kill the greeter.
Have you tried using PackageManager.getLaunchIntentForPackage(..)? This will allow you to send the same Intent that the launcher uses to start your app. It should be equivalent to clicking on your application's launcher icon.
If you do need to go through the launcher, you can use a UiWatcher to dismiss the first-run overlay. Whenever UiAutomator can't find an element, it will call the checkForCondition(..) method for each registered UiWatcher and give you a chance to dismiss any overlays or dialogs that are getting in the way.
Apparently the greeter is called "cling". Searching though (rather old) code I found the following:
http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android-apps/4.0.2_r1/com/android/launcher2/Launcher.java#Launcher.isClingsEnabled%28%29
private boolean isClingsEnabled() {
// TEMPORARY: DISABLE CLINGS ON LARGE UI
if (LauncherApplication.isScreenLarge()) return false;
// disable clings when running in a test harness
if(ActivityManager.isRunningInTestHarness()) return false;
return true;
}
And next stop is isRunningInTestHarness() at http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.0.2_r1/android/app/ActivityManager.java#ActivityManager.isRunningInTestHarness%28%29
public static boolean isRunningInTestHarness() {
return SystemProperties.getBoolean("ro.test_harness", false);
}
Which in turn leads to adb shell setprop ro.test_harness true. Which just works.

How make a Android application that can't be closed

I'm developing an Android app for a company where those who will use it are the employees, for this reason the company asked me to develop an application that the user can not close, because if he will not use the smartphone for other purposes, but for this I need the Android native buttons do not interfere with the application.
I've deactivated the button to go back and put the application as Home.
#Override
     public void onBackPressed () {
         super.onBackPressed ();
     }
...
<category android: name = "android.intent.category.HOME" />
However if the user clicks the button that displays open applications, it can exit the application.
I researched a lot before creating resolve this question and realized several attempts to solve this problem.
One. I tried to create the same behavior as the MX Player has, when you use the lock to see a video, the MX Player is always on top of everything, leaving the user to see and click others places. However using this behavior does not i cant see My Dialogs nor Popup and also can not apply a thema, as in my case is not simply an activity is the entire application.
Reference links of my attempt
How to disable Home and other system buttons in Android?
http://www.piwai.info/chatheads-basics/
If anyone knows how to use that behavior MX Player, or if anyone knows any more how to make the user can not close the application, please help me, I know it's not just me who have this problem.
Any help is welcome!
My API is 16 - Android-4.1
Are your target devices rooted? If so, this article goes through the steps to make this possible. What you specifically ask about can be done by modifying the build.prop file to include the following line: qemu.hw.mainkeys=1. This will stop the soft-key navigation bar from ever showing up.
If the device you're running isn't rooted, I don't think that it's possible to do what you're asking.
The best way i found to the user can't access others apps, was to make a service that check which is the top activity, if don't is my then reopen my app.
ActivityManager manager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> runningTasks = manager.getRunningTasks(1);
if (runningTasks != null && runningTasks.size() > 0) {
ComponentName topActivity = runningTasks.get(0).topActivity;
if (!topActivity.getPackageName().startsWith("com.mypackage.")) {
Log.i("Top Activity", topActivity.getPackageName());
if (LocalCache.getInstance().isForceHome()) {
Intent intent = new Intent(HomeService.this, AuthLoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
handler.postDelayed(this, 500);
}
Old question, but I'm facing exactly same situation:
In-house App
Can't be close
I'm going to set App as a Launcher, and block top-dowm swipe to prevent status bar appear.
I think it's good enough for an in-house App ~

Move back to App after launching another App

I have a situation which I have been working on for close to a week now but can not come up with a working solution.
I have an app, which will launch a terminal window and run a command, the terminal used is Jack Palevich's Android-Terminal-Emulator (source code here) and the code used to launch a window is:
public boolean runCommand(String command) {
Intent intent = new Intent("jackpal.androidterm.RUN_SCRIPT");
intent.addCategory(Intent.CATEGORY_DEFAULT);
intent.putExtra("jackpal.androidterm.iInitialCommand", command);
if (mHandle != null) {
// Identify the targeted window by its handle
intent.putExtra("jackpal.androidterm.window_handle", mHandle);
}
startActivityForResult(intent, REQUEST_WINDOW_HANDLE);
return true;
}
Now this works fine, however the issue is that there are a number of times in the apps life that it will need to pass a command to this terminal window, and no user will be interacting with the device (i.e it will need to be automated).
When you launch a terminal window that window becomes the activity in view, therefore while my app still runs in the background any future calls to runCommand() will not happen until my app is brought back to view.
So I need to find a way to have my app become the app in view again once it has called runCommand() and started the terminal. I have tried a number of routes but with no really success, I guess the only really way forward will be to make changes to the terminal app itself, which I am happy to do (I have the source download and tested) but am just a little stuck as to where to look and what to change.
If you need any more info about my app or anything else, let me know!
Thank you

Android Device Admin dialogue not showing up - Auto declined

I'm trying to implement locking the screen with my app.
I've worked my way through google documentation and studied the sample.
When i call this
void getAdmin(){
Intent activateDeviceAdminIntent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mPolicy.getPolicyAdmin());
activateDeviceAdminIntent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
getResources().getString(R.string.AdminReceiverDescr));
startActivityForResult(activateDeviceAdminIntent, REQ_ACTIVATE_DEVICE_ADMIN);
}
The dialogue doesn't show up (maybe it flashes a little, can't say for sure), it just jumps in my onResult routine and result is "not granted". But my app afterwards shows up in the list in settings/security/device admins and if i enable admin rights manually it locks my screen like a charm.
I've added the permission
uses-permission android:name="android.permission.BIND_DEVICE_ADMIN"
as well as
receiver ... android:permission="android.permission.BIND_DEVICE_ADMIN"
to my manifest.
Does anyone have a clue what i may have missed?
Thx in advance!
Found the problem in my manifest: you have to specify the receiver subclass with a $ sign.
Example:
android:name="com.exampl.PolicyClass$PolicyReceiver"

Categories

Resources