How to use UsageStatsManager on Android Wear - java

I am trying to use UsageStatsManager on an Android Wear device.
What I did:
I am requesting the permission for it on the mobile app that is running on my phone. After the permission was given by the user, I can access the usage stats data.
Then I created a UsageStatsManager on the wear app and tried to access the usage stats data there. However, on the watch, I do not have the permission to do that and I can not ask for permission. To check if the permission was given by the user I used:
private boolean isPermissionGranted(){
try{
PackageManager packageManager = getPackageManager();
ApplicationInfo applicationInfo = packageManager.getApplicationInfo(getPackageName(), 0);
AppOpsManager appOpsManager = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
int mode = appOpsManager.checkOpNoThrow(AppOpsManager.OPSTR_GET_USAGE_STATS, applicationInfo.uid, applicationInfo.packageName);
return mode == AppOpsManager.MODE_ALLOWED;
}catch(PackageManager.NameNotFoundException e){
return false;
}
}
This returns true on the phone but false on the watch.
The problem:
I can ask the user for permission to use UsageStatsManager on the phone but I can not do that on the watch because the setting does not exist. Has anyone figured out if it is possible to get UsageStatsManager running on a watch?

Usage Permission intent isn't possible on Wear.
Same goes for Overlay (Settings.canDrawOverlays()) though at least you can use lower target to overcome it.
Also with latest Android N emulator this isn't resolved.

Related

How force the app to opt out of battery saver mode when the service is ON?

The expected behavior is that the app will be running all the time when it's in ON state. Some phones put the app in background mode when the app is not active for some time. I want the app to be running all the time even its in standby mode(standby mode means when we press the home button the app will go to background. and it will run for some time).
I found following code and I tried that
PowerManager powerManager = (PowerManager) getApplicationContext().getSystemService(POWER_SERVICE);
String packageName = "org.traccar.client";
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Intent i = new Intent();
if (!powerManager.isIgnoringBatteryOptimizations(packageName)) {
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + packageName));
startActivity(i);
}
else{
i.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
i.setData(Uri.parse("package:" + packageName));
startActivity(i);
}
}
Even after working with the code the default state is Battery Saver(recommended)
I want the app in No Restriction mode once the app is opened, any solution for this?
The code you use is for battery optimization. Settings-->Batery-->Three Dots Menu Item (...)--->Battery Optimization-->(Choose an app from list)--->Optimize/ Don't optimize.
By choosing Don't optimize you are essentially bypassing Doze, not app standby.
Also be advised that doing this programmatically as you do may result in Google taking your app off the store. It is safer to do it manually following the path i described above.
More on Doze and App Standby here

Communicating with the USB device plugged into Android Phone (for instance, an external camera)

I would like to know if there is some kind of an API in android studio that enables communication between a device and the android phone through a USB. For example, external camera.
I have used SetupAPI and WINUSB before to accomplish such a task. So something similar to those two would be appreciated.
The company that created the device does not provide an SDK, driver or any extra information.
Thank you very much.
It depends on what you want to do BUT short answer is yes.
To detect an external camera you may try this:
public String getExternalCamera(){
CameraManager cameraManager = (CameraManager) getSystemService(CAMERA_SERVICE);
String exCamId = null;
for (String cameraId : cameraManager.getCameraIdList()) {
CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId);
Integer facing = characteristics.get(CameraCharacteristics.LENS_FACING);
//LENS_FACING_EXTERNAL will return Value: 2
if (facing != null && facing.equals(CameraCharacteristics.LENS_FACING_EXTERNAL)) {
exCamId = cameraId;
}
}
return exCamId;
}
LENS_FACING_EXTERNAL
added in API level 23
public static final int LENS_FACING_EXTERNAL
The camera device is an external camera and has no fixed facing relative to the device's screen.
You can also use:
INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL
added in API level 28
public static final int INFO_SUPPORTED_HARDWARE_LEVEL_EXTERNAL
This camera device is backed by an external camera connected to this Android device.
The device has capability identical to a LIMITED level device, with some exceptions.
For more info see Android documentation here!

NetworkStats in Android 6.0 - Permissions issues

I'm trying to develop an app that can get the Network stats from specific packages, but I'm getting these problems:
When I try to use the NetworkStats Library of Android 6.0(Marshmallow), I get this Exception:
NetworkStats: Neither user 10412 nor current process has
android.permission.READ_NETWORK_USAGE_HISTORY.
Here is the code:
try {
TelephonyManager tm;String subscriberID;
NetworkStatsManager networkStatsManager;
NetworkStats networkStats;NetworkStats.Bucket bucket;
tm = (TelephonyManager) mContext.getSystemService(mContext.TELEPHONY_SERVICE);
subscriberID = tm.getSubscriberId();
networkStatsManager = mContext.getSystemService(NetworkStatsManager.class);
networkStats = networkStatsManager.queryDetailsForUid
(typeMobile, subscriberID, dtBegin, dtEnd, uidPackage);
if(networkStats !=null){
while (networkStats.hasNextBucket()) {
bucket = new NetworkStats.Bucket();
networkStats.getNextBucket(bucket);
Log.d("Bucket RX:",bucket.getUid()+" -" +String.valueOf(bucket.getRxBytes()));
Log.d("Bucket TX:",bucket.getUid()+" -" +String.valueOf(bucket.getTxBytes()));
}
}
}
catch (Exception ex){
Logger.e(ex.getMessage());
}
How can I get the functionality of NetworkStats in previous versions of Android (5.0 & 4.0)? Is there any library?
READ_NETWORK_USAGE_HISTORY permission is only granted for system applications. So, unless you are using a rooted phone, you won't be able to use it. The only way out is to use TrafficStats which is available since API level 8.
Mmm ... After a little bit research over internet I give up on using this Android Native Library. I have managed to make it work, but its necessary that the user enables it through "User Data Access" option (Located under Settings App). Then I try to launch this app to the user so the user can enable by himself, but this doesnt not work on many devices. See this link:
Devices without "Apps using Usage Data" or android.settings.USAGE_ACCESS_SETTINGS intent
enter image description here

how to manage hardware of android cell phone

I am using Android emulator and development platform.
I have created a new application and wonder how can I close or open Android cell phone camera?
If you are talking about opening it from your app you should use the camera Intent for that.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra("android.intent.extras.CAMERA_FACING", 1);
startActivityForResult(intent, 1);
EDIT:
1 - is for the front-facing camera
When you create the AVD for your emulator, there are options for enabling front and back camera. If you have enabled this option for your emulator, you will be able to access the camera just like you would on a regular phone (using the camera app, or via the system).
You probably didn't enable the camera option on your emulator image if you are not seeing it.
I am going to assume you have enabled a camera on your emulator, and want to access it's functions yourself, instead of opening an existing camera application. You'll need to use functions in Android to actually access this hardware, and you'll also need to give the app permission to use it.
private void startCamera(){
//First check if a camera is available
if(getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
Log.d("CameraApp", "It has a camera");
Camera cam = Camera.open(); //Start using the camera. From here on out you should be able to access it's functions.
cam.unlock();
cam.startPreview();
} else {
Log.d("CameraApp", "It does not have a camera");
Toast.makeText(this, "No camera available",
Toast.LENGTH_SHORT).show();
}
}
Be sure to close it when you're done.
private void stopCamera(){
cam.stopPreview();
cam.release();
}
Add this to your AndroidManifest.xml to give your app permission:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
Each function (zoom, autofocus, etc.) needs it's own permission to be added.
Also see this for more information: http://developer.android.com/reference/android/hardware/Camera.html

How can I get all applications that have the internet permission in Android Applications?

How can I get all applications that have the internet permission in Android?
How can I do that?
any idea?
You need to iterate over all installed apps, using the PackageManager.GET_PERMISSIONS flag. You can then check if the internet permission is in the returned value.
Sample code (based on this answer):
PackageManager p = context.getPackageManager();
final List<PackageInfo> apps = p.getInstalledPackages(PackageManager.GET_PERMISSIONS);
for (PackageInfo pkg : apps) {
for (String permission : pkg.requestedPermissions) {
// Check if permission is the internet permission
}
}

Categories