Unlock device, display a text, then lock again - android

For the need of my application, I need to display a message on the screen even if the lockscreen is enabled, then wait 3 seconds, than I have to lock again the phone as I don't want it to make unwanted phone calls in your pockets.
First part is easy:
if (PreferenceManager.getDefaultSharedPreferences(
getBaseContext()).getBoolean("wake", false)) {
KeyguardManager kgm = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
boolean isKeyguardUp = kgm.inKeyguardRestrictedInputMode();
WakeLocker.acquire(ProtoBenService.this);
Intent myIntent = new Intent(ProtoBenService.this,LockActivity.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (isKeyguardUp) {
ProtoBenService.this.startActivity(myIntent);
} else
Toast.makeText(ProtoBenService.this.getBaseContext(), intention, Toast.LENGTH_LONG).show();
WakeLocker.release();
}
With this class:
public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context ctx) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "CobeIm");
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
And the Activity:
public class LockActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
window.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
TextView tv = new TextView(this);
tv.setText("This is working!");
tv.setTextSize(45);
setContentView(tv);
Runnable mRunnable;
Handler mHandler = new Handler();
mRunnable = new Runnable() {
#Override
public void run() {
LockActivity.this.finish();
}
};
mHandler.postDelayed(mRunnable, 3 * 1000);
}
}
So, this is nice, the phone can display my text!
The only problem comes when I want to lock again the phone, it seems that locking the phone is protected by the system...
Programmatically turning off the screen and locking the phone
how to lock the android programatically
I think that my users won't understand the Device Admin and won't be able to activate it. Is there any workaround to lock the screen without the Device Admin stuff?

I used the following method for locking and unlocking phone.
initializing
KeyguardLock keyguard;
KeyguardManager km = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
keyguard = km.newKeyguardLock("MyApp");
to unlock phone
keyguard.disableKeyguard();
to lock phone again
keyguard.reenableKeyguard();
to turn screen on
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
and dont forget to use the following permission
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />

I am pretty sure that you have to use the Device Admin Features to lock the screen.
protected static void initiateDeviceLock(Context context) {
ComponentName componentName = new ComponentName(context, MyDeviceAdminReceiver.class);
DevicePolicyManager dpm = (DevicePolicyManager) context.getSystemService(Context.DEVICE_POLICY_SERVICE);
boolean active = dpm.isAdminActive(componentName);
Log.i(context.getClass().getSimpleName(), "Active (in initiateDeviceLock) = " + String.valueOf(active));
if (active) {
dpm.lockNow();
}
}
To help the user's setup the Device Admin features you can take them to the settings page:
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
ComponentName componentName = new ComponentName(TestActivity.this, MyDeviceAdminReceiver.class);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
startActivityForResult(intent, CODE);

Related

How to lock/unlock the screen with Pattern/Password mode in Android?

I was successful to lock/unlock my screen using DevicePolicyManager and KeyguardManager in Android L. It worked well when I lock/unlock screen using swipe mode (no security). However, I cannot lock/unlock it when I lock/unlock screen by using Pattern and Password mode (higher security). Is it possible to lock/unlock screen with high security using DevicePolicyManager and KeyguardManager. ? This is what I did
protected static final int REQUEST_ENABLE = 0;
DevicePolicyManager devicePolicyManager;
ComponentName adminComponent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(btnListener);
}
//LOCK
Button.OnClickListener btnListener = new Button.OnClickListener() {
public void onClick(View v) {
adminComponent = new ComponentName(MainActivity.this, Darclass.class);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!devicePolicyManager.isAdminActive(adminComponent)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
startActivityForResult(intent, REQUEST_ENABLE);
} else {
devicePolicyManager.lockNow();
}
}
};
//UNLOCK
private KeyguardManager keyguardManager;
KeyguardManager.KeyguardLock kl;
keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
kl = keyguardManager.newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
Note that, I am using it in a service.
You mentioned that you are using the code in a Service, but while constructing your adminComponent ComponentName object you provide MainActivity.this as your context ! MainActivity.this might be NULL if your MainActivity is not currently running.
I suggest you initiate adminComponent with the Service class as the Context.

How do you make WakeLock pop app to foreground

I want my app to run in the background or after the phone has been put to sleep and when you near a certain latitude and longitude my app will immediately pop to the foreground. this must work no matter if it was from sleep or another app was in foreground.
I am using a service and a WakeLock. I am pretty sure the service is working but I don't know how to check because the debugger returns nothing if it is not in the foregrond. If it is working then my WakeLock is not working as anticipated.
public abstract class WakeLocker {
private static PowerManager.WakeLock fullWakeLock;
private static PowerManager.WakeLock partialWakeLock;
private static PowerManager pm;
public static void acquire(Context context) {
if (fullWakeLock != null) fullWakeLock.release();
if (partialWakeLock != null) partialWakeLock.release();
pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
fullWakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP), "Loneworker - FULL WAKE LOCK");
partialWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Loneworker - PARTIAL WAKE LOCK");
partialWakeLock.acquire();
}
public static void release() {
if (fullWakeLock != null) {
fullWakeLock.release();
fullWakeLock = null;
}
if (partialWakeLock != null) {
partialWakeLock.release();
partialWakeLock = null;
}
}
public static void wakeDevice(Context context) {
fullWakeLock.acquire();
//KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
//KeyguardManager.KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
//keyguardLock.disableKeyguard();
}
public static boolean fullWakeLockActive() {
return fullWakeLock != null;
}
}
the fullWakeLock is being acquired here
if (makeUseOfNewLocation(location) < 20) {
if (WakeLocker.fullWakeLockActive())
WakeLocker.wakeDevice(allert.getMA());
if (callProgress.isOffHook()) {
new BlinkBack(allert.getMA());
}
The wake lock itself cannot wake the device from sleep or cause your so to come to the foreground. You would have to use location services or an alarm to wake the device up, then use your wake lock to keep the device awake and trigger a UI component of your app to the foreground.

Lock Android Phone after user dials a phone number

I am developing a security application whereby, if a user dials a phone number that is not frequently called and he has never called before, the user will have to reauthenticate himself. For this purpose I want to lock the phone after checking the phone number.
public class outgoingCalls extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.v("onReceive", "In onReceive()");
if (confidence == 0) {
Log.v("onReceive","confidence zeroed");
Intent i = new Intent();
i.setClassName("abc.xyz.SECURITY","abc.xyz.SECURITY.lockActivity");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
In this new Activity lockActivity, I need to lock the phone where I have commented // LOCK PHONE
public class lockActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("lock","lockActivity onCreate method called");
// setContentView(R.layout.main);
Log.v("lock","locking");
// LOCK PHONE
}
}
The phone is not getting locked with the methods I have tried. These include the following:
1. KeyguardManager mgr = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = mgr.newKeyguardLock("edu.Boston.SECURITY.lockActivity");
((KeyguardLock) lock).reenableKeyguard();
2. PowerManager manager = (PowerManager) getSystemService(Context.POWER_SERVICE);
manager.goToSleep(100);//int amountOfTime
3. PowerManager.WakeLock wl = manager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "Tag");
wl.acquire();
wl.release();
4. WindowManager.LayoutParams params = getWindow().getAttributes();
params.screenBrightness = 0;
getWindow().setAttributes(params);
Android manifest file has below permissions
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
<uses-permission android:name="android.permission.WAKE_LOCK" />
Why am I not able to lock the phone? Any pointers?
Thanks a lot in advance for your help. Appreciate it!
Try creating a DevicePolicyManager
http://developer.android.com/training/enterprise/device-management-policy.html
and then call:
DevicePolicyManager mDPM = (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
mDPM.lockNow();

Disabled Keyguard Lock re-enables itself after clicking on a notification

In my application I disable the keyguard lock (i.e.Remove Lockscreen) using the code below and it works fine until I click on any notification in the notification bar. If I click on a notification the lock screen is automatically re-enabled. Any help is appreciated.
private void remove_lockscreen() {
final CheckBoxPreference lock = (CheckBoxPreference) findPreference("remove_lockscreen");
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
KeyguardLock kl = km.newKeyguardLock("keyguard_lock");
if (lock.isChecked()) {
prefEdit("remove_lockscreen", 1);
Toast.makeText(getBaseContext(), "Lockscreen will not be shown", Toast.LENGTH_SHORT).show();
kl.disableKeyguard();
}
else if (!lock.isChecked()) {
prefEdit("remove_lockscreen", 0);
Toast.makeText(getBaseContext(), "Lockscreen will be shown", Toast.LENGTH_SHORT).show();
kl.reenableKeyguard();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
I've noticed the same issue for some time. It only occurs on Honeycomb (Android 3.0) and up. After a great deal of experimentation and hair-pulling, I seem to have found a solution that works for me. It's not clear exactly what's going on or why, but here's what I've figured out.
It seems that on Android 3.0+, after the keyguard is disabled, when a notification is pressed, the old KeyguardLock expires, but thankfully the ACTION_USER_PRESENT Broadcast is fired at that point, so we have a chance to correct the issue.
One point that's not at all obvious from the documentation is that it seems to be necessary to reenable the old KeyguardLock before getting a new one and disabling it again. Another "gotcha" I discovered is that disabling through the new KeyguardLock immediately after reenabling through the old one produces only intermittent success. I resolved this by waiting 300ms before disabling.
Here's a slightly simplified version of my code; it should be easy to adapt to your app:
private KeyguardLock kl;
private KeyguardManager km;
private final Handler mHandler = new Handler();
private final Runnable runDisableKeyguard = new Runnable() {
public void run() {
kl = km.newKeyguardLock(getPackageName());
kl.disableKeyguard();
}
};
private void setEnablednessOfKeyguard(boolean enabled) {
if (enabled) {
if (kl != null) {
unregisterReceiver(mUserPresentReceiver);
mHandler.removeCallbacks(runDisableKeyguard);
kl.reenableKeyguard();
kl = null;
}
} else {
if (km.inKeyguardRestrictedInputMode()) {
registerReceiver(mUserPresentReceiver, userPresent);
} else {
if (kl != null)
kl.reenableKeyguard();
else
registerReceiver(mUserPresentReceiver, userPresent);
mHandler.postDelayed(runDisableKeyguard, 300);
}
}
}
private final BroadcastReceiver mUserPresentReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (Intent.ACTION_USER_PRESENT.equals(intent.getAction())){
if (sp_store.getBoolean(KEY_DISABLE_LOCKING, false))
setEnablednessOfKeyguard(false);
}
}
};

How to keep Screen On while looping urls in the Browser.apk?

Currently I am using IntentService to loop urls in the Browser.apk. I am running it until the battery drains. Here is my dirty code. =)
#Override
protected void onHandleIntent(Intent intent) {
int size = intent.getStringArrayExtra("addresses").length;
int counter = sp.getInt("counter", 0);
String address = intent.getStringArrayExtra("addresses")[counter];
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(address));
i.setFlags(Intent.FLAG_FROM_BACKGROUND | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
counter++;
if(counter == size) {
counter = 0;
}
spe.putInt("counter", counter);
spe.commit();
}
I tried to use wakelock but not all devices stays awake. It is working with Motorola Xoom but not in Thinkpad tablet slate.
Do you know other option other than using the wakelock. Or How should I properly implement the wakelock?
Can I tell the Browser that I should keep the Screen On while loading the urls? By using intent or other means.
For open the device screen, if the device is in sleep mode, use the code below:
//acquireLock(context);
PowerManager pm = (PowerManager)context.getSystemService(Context.POWER_SERVICE);
boolean isScreenOn = pm.isScreenOn();
Log.e("screen on.................................", ""+isScreenOn);
if(isScreenOn==false)
{
WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |PowerManager.ACQUIRE_CAUSES_WAKEUP |PowerManager.ON_AFTER_RELEASE,"MyLock");
wl.acquire(10000);
WakeLock wl_cpu = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"MyCpuLock");
wl_cpu.acquire(10000);
}
Best solution here: https://stackoverflow.com/a/2134602/1316372
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

Categories

Resources