How to run Device Police Manager From A Service - android

I am able to create an Activity that uses the DevicePolicyManager API's.
The tutorials show that I need use it the following fashion:
if (!mDPM.isAdminActive(mAdminName)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminName);
intent.putExtra("wipe-data", DeviceAdminInfo.USES_POLICY_WIPE_DATA);
startActivityForResult(intent, REQUEST_ENABLE);
}
else {
mDPM.wipeData(0);
}
However I would like this to run inside a Service.
But I cant call
startActivityForResult
from within a Service.
So what would be the best approach or strategy for me to try ?

The only reason you need to call startActivityForResult() is if your app is not presently configured as a device administrator, to lead the user to go set that up for you. Hence, put that portion of your logic inside of your user interface.
Your service itself would just skip doing anything if isAdminActive() returns false.

Related

How can I check the expected intent sent without actually launching activity in Espresso?

I have a UI test which clicks a button, and then launch a new Activity in its onClickListener. The test checks whether expected intent is sent or not.
My problem is, I want to test whether expected intent is sent without actually launching the activity. Because I found that new activity initializes its state, and it makes subsequent tests flaky.
I know there are two Espresso Intents API, which are intended and intending, but both fail to meet my needs. intended API actually launches the target activity, and intending API doesn't launch the activity, but it calls onActivityResult callback which I don't want either. Because I'm afraid that code inside onActivityResult may cause another flakiness.
Also intending doesn't assert whether matching intent is sent. It just calls onActivityResult callback when matching intent is found, which means I have to check whether onActivityResult is called or not!
Is there a clean way to achieve what I want?
If you want to test whether expected intent is sent without actually launching the activity you can do it by capturing the intent with an activityResult and then catching the activity :
Intent intent = new Intent();
ActivityResult intentResult = new ActivityResult(Activity.RESULT_OK,intent);
intending(anyIntent()).respondWith(intentResult);
onView(withId(R.id.view_id_to_perform_clicking)).check(matches(isDisplayed())).perform(click());
intended(allOf(hasComponent(ActivityToBeOpened.class.getName())));
This would catch any attempt of launching ActivityToBeOpened. If you want to be more specific you can also catch an intent with Extras:
intended(allOf(hasComponent(ActivityToBeOpened.class.getName()), hasExtra("paramName", "value")));
Hope that helps.
Espresso's Intents class is a concise and handy api, but when it doesn't meet your needs, there is an alternative. If you use AndroidJUnit4 test runner, you can get Instrumentaion instance using InstrumentationRegistry.getInstrumentation(), and then you can add Instrumentation.ActivityMonitor instance.
Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor("YOUR_ACTIVITY", null, true);
InstrumentationRegistry.getInstrumentation().addMonitor(am);
onView(withId(R.id.view_id_to_perform_clicking)).check(matches(isDisplayed())).perform(click());
assertTrue(InstrumentationRegistry.getInstrumentation().checkMonitorHit(am, 1));
The third parameter of ActivityMonitor constructor tells we want to block activity launching. Note that this approach has its limitation. In contrast to Espresso Intents' rich Matcher support, You can not set multiple condition for ActivityMonitor.
You can find several samples in ApiDemos, especially in ContactsSelectInstrumentation class.
Actually, you can block any intent to launch an external or your own activity but still use the rich Espresso Intents API:
Instrumentation.ActivityMonitor soloMonitor = solo.getActivityMonitor();
instrumentation.removeMonitor(soloMonitor);
IntentFilter filter = null;
// Block any intent
Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(filter, null, true);
instrumentation.addMonitor(soloMonitor);
// User action that results in an external browser activity being launched.
user.clickOnView(system.getView(R.id.callButton));
instrumentation.waitForIdleSync();
Intents.intended(Matchers.allOf(
IntentMatchers.hasAction(Matchers.equalTo(Intent.ACTION_VIEW)),
IntentMatchers.hasData(Matchers.equalTo(Uri.parse(url))),
IntentMatchers.toPackage(chromePackage)));
instrumentation.removeMonitor(monitor);
You able to do that because Espresso Intents still records every Intent with IntentMonitor callback even if you block them. Look at the source code of Espresso Intents on how they do that.
If you use Robotium Solo framework you need to move your own ActivityMonitor before their one. Otherwise just skip the lines related to this.

Test activity from another task

I am trying to test my activity which has a button to go to a People application with ActivityInstrumentationTestCase2. But the problem is People application starts edit activity in another task, and clearly I don't have access there. So, what to do? How to send signals to People application?
My intent is:
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, "");
intent.putExtra(ContactsContract.Intents.Insert.PHONE, "");
Well, I figured it out, pretty sure other developers will find it useful.
First of all, to enable testing interaction with People application you can prefill the contact data with related Intent fields:
public static void addAsContactConfirmed(final Context context, final Person person) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, person.name);
intent.putExtra(ContactsContract.Intents.Insert.PHONE, person.mobile);
intent.putExtra(ContactsContract.Intents.Insert.EMAIL, person.email);
intent.putExtra("finishActivityOnSaveCompleted", true); // this line enables instant return People to our app
context.startActivity(intent);
}
Then you need to press back button, and this is the tricky part:
You can't just finish(), because it is not your activity, and even not the same task
You can't access any views of People app by the same reason
You cry a lot, because you don't know, what to do
But here I am with the solution. You need to use UiAutomation which can be obtained from instrumentation by calling final UiAutomation uiAutomation = getInstrumentation().getUiAutomation();.
Then you can't send any key events to another application, because it requires INJECT_EVENTS permission, which needs system signature, which your application apparently lacks :)
So, call this one:
uiAutomation.performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
It does not require any permissions and just work across application. Be sure you run your test above Android 4.3.
Additional link.

Android: getting info on the calling app from activity

I want to implement a fine-granularity protection mechanism in an exported activity. The permissions framework does not seem to work for my requirements.
There are two options I am considering:
using Activity.getCallingPackage - only works if the activity is started with startActivityForResult - this is a limitation I would like to avoid if possible.
using Binder.getCallingUid - when called in an Activity, it returns the local UID, and not the calling UID.
Is there any way to allow activities started with startActivity to retrieve any information about the calling app?
You can add extra information in your intent.
Intent i = new Intent(this, NextClass.class);
i.putExtra("extra", "This is some extra information";
startActivity(i);
You retrieve the data from NextClass by:
Intent i = getIntent();
String extraStuff = i.getStringExtra("extra");
Is there any way to allow activities started with startActivity to retrieve any information about the calling app?
No, sorry, beyond your startActivityForResult() hack. Android's support for "a fine-granularity protection mechanism" is designed around services, not activities or other components.

How do we find the information for launching other app?

I need to allow user to draw/sketch/paint something. There are already many apps(like Skitch, I will use this as an example) that accomplish this task. So I don't want to re-invent the wheel.
In Android, theoretically, we can launch other activity by intent. This is sort of like "pipe" in Unix.
The problem is, I don't know how to get the information for launching Skitch.
To integrate Skitch in my app, I need to know the Action it supports, the returning intent (if any) when it finishes.
I installed Skitch, photoshop, and lots of other touch drawing apps in my device, but this code doesn't work :
Uri data = Uri.fromFile(file);
Intent i = new Intent(Intent.ACTION_EDIT);
i.setData(data);
i.setType("image/*");
startActivityForResult(i, ACTIVITY_DRAW);
I can launch Skitch from my app in the following way: but obviously I can't get any returned result this way(code from here).
Intent i = new Intent(Intent.ACTION_MAIN);
PackageManager manager = getPackageManager();
i = manager.getLaunchIntentForPackage("com.evernote.skitch");
i.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(i);
My question: Is there a standard way to find information for launching a third party app?
Is this site the only way to share/get such information?
Or if you have any suggestions for my problem, please help me.
Thank you!
As you might already know how to call another application Activity from your app ..this way Mentioned Here.
Intent intent = new Intent(Intent.ACTION_RUN);
intent.setComponent(new ComponentName("<packet name>", "<class name>"));
List list = packageManager.queryIntentActivities(intent, packageManager.COMPONENT_ENABLED_STATE_DEFAULT);
if(list.size() > 0)
{
Log.i("Log", "Have application" + list.size());
startActivity(intent);
}
else
{
Log.i("Log", "None application");
}
All your require is Mainly Two Things to call any Activity
1) Package Name of that Activity
2) Activity Class Name
These two informations only can be available if they are opensource or made free to use .. like Zxing,Google Maps Application.
There is another way to start an application activity like,
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + numberField.getText())); // set the Uri
startActivity(intent);
For this way to use need to know the correct Action for the Activity you want to call and the Correct parameter to pass with.
And again,These information only can be available if they are opensource or made free to use .. like Facebook and Gmail apps to share and post messages.
So If you are searching for anything like which can tell you what you will need to pass to call any specific comercial apps in your device, you wont find it directly.
It's an old question but perhaps it could help somebody to know that Sony's AppXplore application (free) shows the package and name of the activities of every app installed on your device, so you can eventually use them to do explicit Intents.

About calling in android phone

I want make simple call app.
I use this code.
Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:13643569345"));
startActivity(intent);
It does work well, but I don't want to use startActivity. it changes activity which system default call activity.
How can I call without using startActivity()?
How can I call but don't use startActivity().
You can't. There is no means to initiate a phone call except via startActivity(), except via custom firmware.

Categories

Resources