Lock The Android device screen and prevent the user to unlock it - android

I am working in Android App that should prevent the user to use the mobile in some-cases .
So I tried to lock the screen
I used the PowerManger goToSleeo() Method
but it needs DEVICE_POWER permission. which is allowed only for the System apps, but my app is not a system app
what should I do ?
here is my code
PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
manager.goToSleep(600000);

Counterquestion: What purpose would it serve if normal apps could lock your screen? In my eyes, that's malware. You need the permission and nothing will ever change that. The only solution is to remove this "functionality".
Edit: Some more information by the way: Android What permissions required to call PowerManager.goToSleep(n) put device in sleep mode?

This sort of thing is difficult to do in Android for reason. You are trying to block access to the main OS, which is a bad thing. As other people have mentioned this could be used for malicious purposes (it is not a stretch to think someone could create a ransom-ware app that blocks your device, until you pay something to release it).
So bottom line - you CANNOT do what you are asking (and for good reasons). Especially on a non-rooted phone. One a device is rooted, you CAN do anything (including blocking access to the system buttons).
For more details about this, look into 'Kiosk' mode, or blocking system access (there are many SO questions about this).

You don't want to lock the device, that's deliberately designed against. You can, however, disable touch input by overriding onTouchEvent.
You then need to create a view, like so:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<your code here>
<Disabletouch
android:id="#+id/black_hole"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
And define it like so:
public class DisableTouch extends View {
private boolean touch_disabled=true;
#Override
public boolean onTouchEvent(MotionEvent e) {
return touch_disabled;
}
public disable_touch(boolean b) {
touch_disabled=b;
}
}
Call it in the activity:
(DisableTouch) black_hole = findViewById(R.id.black_hole);
black_hole.disable_touch(true);
And reverse:
black_hole.disable_touch(false);

Related

Prevent application with SYSTEM_ALERT_WINDOW from obscuring my application

Is there any way to assure that my application's window is not obscured by any other application's view using with SYSTEM_ALERT_WINDOW permission?
If not, then is there any better way to assure that my app is not obscured by such window apart from obtaining the same permission and refreshing/showing/whatever my own view (of course shown in alert window) every 100ms or so to keep it visible?
Eventual flickering, in case my application is obscured, is actually a good thing and an indicator to the user that something is wrong.
EDIT: It seems that there is no way to do it except from going through KNOX on Samsung or some other proprietary solution for Trusted UI. Accepted answer is enough for my purpose, but it is not an answer for the question asked.
Even if it's not exactly what you're asking, the closest replacement I know of is:
Either setting android:filterTouchesWhenObscured="true" in your layout (touch events will be filtered and not reach your View if they are going through an overlay, regardless is transparent or opaque). See View#setFilterTouchesWhenObscured(boolean),
Or overriding View#onFilterTouchEventForSecurity(android.view.MotionEvent) and checking for FLAG_WINDOW_IS_OBSCURED. See View#onFilterTouchEventForSecurity(android.view.MotionEvent).
Later can be implemented like so:
override fun onFilterTouchEventForSecurity(event: MotionEvent): Boolean {
if ((event.flags and MotionEvent.FLAG_WINDOW_IS_OBSCURED) == MotionEvent.FLAG_WINDOW_IS_OBSCURED) {
Toast.makeText(context, "Screen overlay detected!", Toast.LENGTH_LONG).show()
return false // touch event is cancelled
}
return super.onFilterTouchEventForSecurity(event)
}
See also the Security section of View class documentation.
Notice that this functionality is available from API 9+. A workaround for older APIs can be found in this SO Question: Analogue of android:filterTouchesWhenObscured for API level below 9.

Android fingerprint auth through alertdialog?

I want to have the user use fingerprint authentication to allow them to perform an action within my app. I have already performed the necessary check, does the hardware exist, is a fingerprint registered etc when they say they would like to use fingerprint auth.
An alertdialog currently opens when it is time for the user to authenticate with their fingerprint. I'd like to know if it is actually possible to catch the fingerprint through an alertdialog as and alertdialog afaik only has positive and negative button input options.
If it is not possible to do this through an alertdialog, a point in the right direction would be much appreciated.
EDIT: Just to be clear, I don't mean using the screen as a fingerprint sensor.
You can use this FingerprintDialog library. Then it goes simply like this :
FingerprintDialog.initialize(this)
.title(R.string.title)
.message(R.string.message)
.callback(new FingerprintCallback({
#Override
public void onAuthenticationSuccess() {
// Fingerprint recognized
}
#Override
public void onAuthenticationCancel() {
// User pressed cancel button
}
}))
.show();
Otherwise, you just have to create a custom xml layout, get the view using a LayoutInflater and call setView(view) on the dialog. Google it.
This currently isn't possible. Or if it is, I really doubt it. Most touch screen phones use a capacitive touch. Which means, since our fingers conduct electricity, we disturb the electric fields infront of the phone's screen. While it does support multitouch, I really doubt it's so precise that it detects the ridges on our fingers.
You would need more of a heat sensitive touch screen. Which I don't think they use in most new generation touch screens.
So you could use this API https://developer.android.com/reference/android/hardware/fingerprint/FingerprintManager.html but only in device that contains fingerprint scan and definitely not through the screen of you device.

How to listen to taps and get views using Accessibility in Android?

I want to implement an Accessibility service in my Android app which can do the following things:
=>Get the onscreen taps/clicks across any apps.
=>Get the view which was tapped/clicked.
Initially I thought it would not be possible to do so because of security reasons but while doing some research I came across an app (Native clipboard) which could do following things:
=>Detect taps on EditTexts from any app
=>Add value (string) to those EditTexts.
I also saw Google's talkback which speaks whatever you tap on. For it to speak, it needs to access the view (to get the text) across apps.
These apps obviously makes use of 'Accessibility services' to do so, but I would like to know how can I implement that?
I mostly find tutorials or stuffs for things I need to achieve but I am struggling finding some for implementing Accessibility service for my app. I did visit the Android official documentation which is too technical for a newbie like me. (I initially prefer to learn from Youtube, SO and tutorial websites). It will also be great if you can pin point me to some other tutorials which covers these things.
Accessibility services are pretty poorly documented, but I have created some accessibility service boilerplate code, that sets up a starter project and logs the base callbacks. Here is a bit of code that I think you care about given your specific questions. The scaffolding, project set up and such I leave for the repo.
Below is the onAccessibilityEvent callback. This is where you will listen for different types of a events, and the most convenient place to grab onto screen content for most scenarios. Though, as an accessibility service you also don't have to wait for events. You could just as easily spawn an AsynTask and grab on to it on an interval of some kind.
public void onAccessibilityEvent(AccessibilityEvent event) {
CLog.d(event.toString());
switch (event.getEventType()) {
//On Gesture events print out the entire view heirarchy!
case AccessibilityEvent.TYPE_GESTURE_DETECTION_START:
CLog.d(A11yNodeInfo.wrap(getRootInActiveWindow()).toViewHeirarchy());
case AccessibilityEvent.TYPE_VIEW_CLICKED:
CLog.d(event.getSource().toString());
default: {
//The event has different types, for you, you want to look for "action clicked"
if (event.getSource() != null) {
CLog.d(A11yNodeInfo.wrap(event.getSource()).toViewHeirarchy());
}
}
}
}
I will point out one bit of configuration for this, because it is super important. Accessibility services are best configured through an XML file connected to your service through the Manifest file. The contents of this file are:
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:description="#string/accessibility_service_description"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagReportViewIds"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:settingsActivity="com.moba11y.basicaccessibilityservice.SettingsActivity"
/>
For you the important bits are canRetrieveWindowContent="true" and accessibilityEventTypes="typeAllMask". A design bit I like, is ensuring that your grabbing onto the minimal set of event types you want. Different Accessibility Events report broadly different results. For example, many events return "null" from getSource(). This forces you to add a lot of filters for this, or risk null pointer exceptions. It's quite annoying.
The last bit you need is Accessibility Actions. This is what allows you to simulate clicks, long clicks AND add text to an editable text view. Below is code that allows you to do this.
public void onAccessibilityEvent(AccessibilityEvent event) {
AccessibilityNodeInfo source = event.getSource();
if (source != null & event.getClassName().equals("android.widget.EditText")) {
Bundle arguments = new Bundle();
arguments.putCharSequence(
AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE,"some value");
source.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
}
}
https://github.com/chriscm2006/Android-Accessibility-Service-Boilerplate

Best Practices for organizing different code for phone and tablet

I'm adapting an existing phone application for tablets. The problem is that there are some small differences in the UI between the phone and the tablet.
For example, on the phone, there is a landing page and then a login page with a cancel button that goes back too the landing page.
On the tablet, the login fragment is on the landing page and the cancel button is removed. This means that I've made a check to see if the device is a tablet and if it is, I dunno find the view of the cancel button.
This seems hacky to me and i was wondering if there was a better way to do this. Thanks.
Tablet or phone ?
First of all you should know on which device you are. An elegant way (in my opinion) is to declare a resource in config.xml :
values/config.xml
<bool name="isTablet">false</bool>
values-sw600dp/config.xml
<bool name="isTablet">true</bool>
Then extends Application and keep the type of device running the app :
public static boolean IS_TABLET = false;
public void onCreate() {
super.onCreate();
MyApp.IS_TABLET = getResources().getBoolean(R.bool.isTablet);
}
Handling differents view
To handle differents view use the differents folders in /res
/layout for phone view
/layout-sw600dp for 7" tablet (you can just use this folder if there is no difference between 7 et 10")
/layout-sw720dp for 10" tablet
Handling code
Two solutions here :
1- The change between views are minor : keep the same activity/fragment and add some condition like
if(MyAPP.IS_TABLET) {
// DO something on tablet
} else {
// Do something on phone
}
2- If tablet and phone are very different create a new activity/fragment with a suffixe like :
HomeActvity => HomeActivityTablet
And add a condition on the loading of this particular view.
You can also works with differents namespace , depending on what give you best architecture.
Exemple
Have a look on the Google IO app's source code

Android 3.0 Use Physical Keyboard Setting

Background:
I recently purchased a Motorola XOOM Tablet along with the Desktop Dock and Bluetooth Keyboard accessories.
The dock and keyboard work great, but when I take the tablet off the dock to move away from my desk, the keyboard still remains paired with the device and I have to manually change the settings to use the soft keyboard. The same goes for when I set it back on the dock, I need to manually switch it back. It's not a huge problem, but it would be nice not to have to think about it.
So I tried downloading an app from the market that simply toggled Bluetooth on and off when connected or disconnected from a power source, which worked well for a while, but the background service would die after period and become useless until I manually restarted that.
TO THE POINT: I'm trying to write a little app/service for my tablet that will recognize when it has been docked/undocked and switch the "Use Physical Keyboard" setting accordingly.
I have started with a BroadcastReciever to recognize the Dock State:
public class DockBroadcastReciever extends BroadcastReceiver {
private final String DOCK_STATE_LABEL = "android.intent.extra.DOCK_STATE";
#Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
String message = (extras.getInt(DOCK_STATE_LABEL) == Intent.EXTRA_DOCK_STATE_UNDOCKED) ? "Undocked" : "Docked";
Toast toast = Toast.makeText(context, message, Toast.LENGTH_LONG);
toast.show();
}
}
But I'm having trouble figuring out the best way to update the setting after the event is fired. I've poked around some examples using InputMethodManager, but all the methods seem to need a specific EditText or some other input to bind to.
Furthermore, I can't seem to find a corresponding constant that represents that setting anywhere in the docs, but graphically, it is located here: http://i.stack.imgur.com/esFaw.png
Can anyone help me out with this?
I would like for there to be a solution for changing the setting, but I am open to other ideas as well.
I have an app that does something similar. It can toggle wifi and bluetooth based on power.
You'll need to register some of this stuff in the AndroidManifest.xml file.
http://code.google.com/p/futonic-wifioncall/source/browse/AndroidManifest.xml
Project Open Source Site: http://code.google.com/p/futonic-wifioncall/
This isn't the solution but hopefully will give guidance on what you're trying to accomplish.

Categories

Resources