Android: Can't re-enable the keyguard after disabling it - android

I'm trying to create an option (using check box preferences) in my app for the user to disable and re-enable the lock screen. I use disableKeyguard() to disable the lock screen and it works flawlessly but I can't get reenableKeyguard() to work. The code is pretty simple, I don't know why it's not working.
public void onSharedPreferenceChanged(SharedPreferences taskprefs,
String tasks_pref) {
boolean skiplock = taskprefs.getBoolean("pref_skiplock", false);
boolean screentimeout = taskprefs.getBoolean("pref_screentimeout",
false);
skiplock(skiplock);
// Log.v("TaskActivity", "Skiplock value is " + skiplock);
// Log.v("TaskActivity", "ScreenTimeout value is " + screentimeout);
}
private void skiplock(boolean action) {
KeyguardManager keyguardManager = (KeyguardManager) getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
//
if (action == true) {
lock.disableKeyguard();
Toast.makeText(getApplicationContext(), "Lockscreen Disabled",
Toast.LENGTH_SHORT).show();
}
//
else if (action==false) {
lock.reenableKeyguard();
Toast.makeText(getApplicationContext(), "Lockscreen Enabled",
Toast.LENGTH_SHORT).show();
}
}

The issue was that a new Keyguard object is created (declared) inside the skiplock() method every single time it is called. A simple solution is to declare the keyguard as a global object and refer to that single object so its status does not get reset when the method finishes executing.
In short, just use "KeguardLock lock" and "KeyguardManager keyguardManager" as global constructors.

Related

Unlock android device not working while call ACTION_CALL intent

I am receiving a push notification, On that, calling a foreground service.
From the service, I am calling one Activity.
Here, I have 2 functionality.
1. Sound alarm for emergency
2. Call using ACTION_CALL.
Both are working fine if device unlocked.
But if a device is locked with password or pattern it did not work when push receives.
Below code to unlock the device. this method is called from onStart.
private void unlockDevice() {
KeyguardManager loKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
Window loWindow = this.getWindow();
if (Common.isAboveAPI27()) {
setShowWhenLocked(true);
setTurnScreenOn(true);
} else if (Common.isAboveAPI26()) {
loWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
loWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
dismissKeyguard(loKeyguardManager);
} else {
if (loKeyguardManager != null) {
KeyguardManager.KeyguardLock loKeyguardLock = loKeyguardManager.newKeyguardLock("FullWakeUps");
loKeyguardLock.disableKeyguard();
}
loWindow.addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD); // Deprecated in 26
loWindow.addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED); // Deprecated in 27
loWindow.addFlags(WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON); // Deprecated in 27
}
//Keep screen on
loWindow.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
#RequiresApi(api = Build.VERSION_CODES.O)
private void dismissKeyguard(KeyguardManager loKeyguardManager) {
if (loKeyguardManager != null) {
loKeyguardManager.requestDismissKeyguard(SOSCallAndAlarmActivity.this, new KeyguardManager.KeyguardDismissCallback() {
#Override
public void onDismissError() {
super.onDismissError();
Log.i(TAG, Build.VERSION.SDK_INT + " : onDismissError");
}
#Override
public void onDismissSucceeded() {
super.onDismissSucceeded();
Log.i(TAG, Build.VERSION.SDK_INT + " : onDismissSucceeded");
}
#Override
public void onDismissCancelled() {
super.onDismissCancelled();
Log.i(TAG, Build.VERSION.SDK_INT + " : onDismissCancelled");
}
});
}
}
The below method is call in onDestroy to reenable the lock:
private void reEnabledKeyguard() {
KeyguardManager loKeyguardManager = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if (loKeyguardManager != null) {
KeyguardManager.KeyguardLock loKeyguardLock = loKeyguardManager.newKeyguardLock("FullWakeUps");
loKeyguardLock.reenableKeyguard();
}
Window loWindow = this.getWindow();
loWindow.addFlags(WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON);
}
Code to initiate the call.
public void callOnNumbers(String fsPhoneNumber) {
Intent loCallIntent = new Intent(Intent.ACTION_CALL);
loCallIntent.setData(Uri.parse("tel:" + fsPhoneNumber));
//callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // tried this but not helped.
if (ActivityCompat.checkSelfPermission(CallAndAlarmActivity.this,
android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "call phone permission not granted");
return;
}
startActivity(loCallIntent);
}
Strange is when this method opens the call screen blink and again displays the password screen on lock screen simply press back button I can see the call screen.
I need to know one more thing, even I set all app killing option and disabled battery optimization. the same code did not execute on push receive.
When device inactive half an hour and if push receives, the above code did not even turn on light. when I click on the lock/unlock button I can see my screens properly. even I press it after 30 seconds of push receives time.
The problem facing from android N.
Additional
When the ACTION_CALL intent calls it to execute my activities onPause and I did not add any code in onPause and I can see one error in logcate
2020-04-27 16:23:47.400 25826-25826/app.safety E/ActivityThread: Performing stop of activity that is already stopped: {app.safety/app.safety.CallAndAlarmActivity}
java.lang.RuntimeException: Performing stop of activity that is already stopped: {app.safety/app.safety.CallAndAlarmActivity}
at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:4089)
at android.app.ActivityThread.handleStopActivity(ActivityThread.java:4177)
at android.app.ActivityThread.-wrap24(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1648)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6687)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:810)
2020-04-27 16:23:47.400 25826-25826/app.safety E/ActivityThread: ActivityClientRecord{paused=true, stopped=true, hideForNow=false, startsNotResumed=false, isForward=false, pendingConfigChanges=0, onlyLocalRequest=false, preserveWindow=false, Activity{resumed=false, stopped=true, finished=false, destroyed=false, startedActivity=false, temporaryPause=false, changingConfigurations=false}}
Thanks
I wonder if it has something to do with the Background Execution Limit. in Android. I would recommend looking at this article.
https://medium.com/exploring-android/exploring-background-execution-limits-on-android-oreo-ab384762a66c
I hope this is of any help.

Toggeling Touch by external keyboard

I am building a firmware for visually disabled people. I have to disable touch screen as and when the external key board is connected. And toggle it with Alt + T . For this I have a static volatile flag in View class called misTouchScreenEnabled. Upon plug in of external keyboard I return false from dispatchTouchEvent(). In View Class :
/*
* A register to hold the status of touch screen
*
*{#hide}
*/
public static volatile boolean misTouchScreenEnabled = false;
public boolean dispatchTouchEvent(MotionEvent event) {
if(event == null)
{
return false;
}
Log.d ("CnxsTA","View :: Into dispatchTouchEvent");
// Added by Harsh Vardhan 05102013
Configuration config = getResources().getConfiguration();
if (config.keyboard != Configuration.KEYBOARD_NOKEYS)
{
// And if the device has a hard keyboard, even if it is
// currently hidden, don't pass the touch events to the view
if(!mIsTouchScreenEnabled)
{
Log.d ("CnxsTA","View :: Into dispatchTouchEvent :: Returning False");
return false;
}
}
// Added By Harsh Vardhan
// Some Other Code...
}
In Launcher I catch the ALT + T in dispatchTouchEvent() and toggle the flag using reflection:
case KeyEvent.KEYCODE_T:
{
//Added By Harsh Vardhan 31052013
Log.d ("CnxsTA","Launcher :: onKeyDown :: T pressed");
if ((event.getMetaState() & KeyEvent.META_ALT_ON) == KeyEvent.META_ALT_ON)
{
Object s;
try
{
Log.d ("CnxsTA","Launcher :: In onKeyDown :: In isAltPressed");
s = ToogleTouchScreen(Class.forName("android.view.View"), "mIsTouchScreenEnabled");
if (s instanceof Boolean)
{
boolean v = ((Boolean) s).booleanValue();
//do something
Log.d ("CnxsTA","Launcher :: onKeyDown :: In isAltPressed :: Toggling touchscreen enable flag :: " + v);
if (accessibilityEnabled)
{
if (mTts != null)
{
if (v)
mTts.speak("Touch Screen enabled", TextToSpeech.QUEUE_FLUSH, null);
else
mTts.speak("Touch Screen disabled", TextToSpeech.QUEUE_FLUSH, null);
}
}
}
else if (s == null)
{
//do something
Log.d ("CnxsTA","Launcher :: onKeyDown :: S is Null");
}
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
//return super.dispatchKeyEvent(event);
return true;
//break;
//Added By Harsh Vardhan 31052013
}
This works well for the Launcher and its workspace, ie. the touch gets enabled and disabled when ALT + T is pressed. But the Panel in the bottom containing the back, home, recent apps and notifications remains unresponsive on touch and so is the UI in the application; though in Exploration Mode the Icons names are spoken on touch but the touch is not implemented. And it all retains to normal sate once the external keyboard is removed.
I think the reason behind this could be that the StatusBar Class and Other Classes extending the View which are taking the flag to be false only has cached the whole class or the variables and methods and is not referring to main memory where the flags have been toggled. I am aware that I have made the flag volatile.
I know that my flag gets toggle as I could confirm this from logcat. Please suggest me the direction I could solve this. Thanking in advance.

Changing the Vibrate settings in Jelly Bean, Android

I am looking for how we change the vibrate settings in Jelly Bean. I found that all the pre-JB vibrate settings have been deprecated, but don't see any new AudioManager.[change the vibrate settings] code anywhere. There is a setting "Vibrate when ringing" which I would like to know how to play with.
Thanks for you help.
In android4.1 you can use this to control "vibrate & ringing"
Settings.System.putInt(mContentResolver, Settings.System.VIBRATE_WHEN_RINGING, enable ? 1 : 0);
From the Documentation:
This method is deprecated.
Applications should maintain their own vibrate policy based on current ringer mode that can be queried via getRingerMode().
Seems like Google wants Vibration settings to be handled by each app for itself on an app to app basis, adjusting it's settings by querying getRingerMode().
Settings.System.VIBRATE_WHEN_RINGING is declared with #hide annotation so you may have troubles to use it in Android Studio.
So maybe you have to replace Settings.System.VIBRATE_WHEN_RINGING by its value : "vibrate_when_ringing"
It is annoying that it is declared with the #hide annotation, because it means that Google don't want external developpers using it...
This issue has plagued me for a couple of days now. Finally got it working. Make sure that you have the right permissions as well.
uses-permission android:name="android.permission.WRITE_SETTINGS"/
protected void onCreate(Bundle savedInstanceState) {
audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
}
Then in my on click listener I utilized the following:
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (vibrate == 1) {
try {
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 1);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on ring:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_ON); //Set it to never vibrate on notify
boolean vibrateWhenRinging;
vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "System vibrate error", Toast.LENGTH_LONG).show();
}
} else {
try {
Settings.System.putInt(getContentResolver(), "vibrate_when_ringing", 0);
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_RINGER, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on ring:
audioManager.setVibrateSetting(AudioManager.VIBRATE_TYPE_NOTIFICATION, AudioManager.VIBRATE_SETTING_OFF); //Set it to never vibrate on notify
boolean vibrateWhenRinging;
vibrateWhenRinging = (Settings.System.getInt(getContentResolver(), "vibrate_when_ringing") == 1);
Toast.makeText(MainActivity.this, Boolean.toString(vibrateWhenRinging), Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, "System vibrate error in else statement", Toast.LENGTH_LONG).show();
}
}
}

Disable Screen Lock(Power) Button in Android

I want that when my application is running the power button (which upon pressing locks the screen & screen goes BLACK), should be disabled. So that the user cannot lock the screen.
I have noticed this thing in Samsung Galaxy S phone's Default Camera App. That's the same reason I am trying to do the same. I have also a Camera related App.
try this one
int val=android.provider.Settings.System.getInt(getContentResolver(),
SCREEN_OFF_TIMEOUT);
android.provider.Settings.System.putInt(getContentResolver(),
SCREEN_OFF_TIMEOUT, -1);
Toast.makeText(this, "Disabled Screen Timeout", Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor = settings.edit();
editor.putInt("ScreenTimeout",val);
editor.commit();
}
} catch(Throwable er) {
Toast.makeText(this, "Error "+er.getMessage(), Toast.LENGTH_LONG).show();
}
that will set screen off
to disable key guard in android use
KeyguardManager keyguardManager = (KeyguardManager)getSystemService(Activity.KEYGUARD_SERVICE);
KeyguardLock lock = keyguardManager.newKeyguardLock(KEYGUARD_SERVICE);
lock.disableKeyguard();
and use permition
<uses-permission android:name="android.permission.DISABLE_KEYGUARD"/>
to keep screen alive
#Override
protected void onCreate(Bundle icicle) {
super.onCreate(icicle);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
I have reached this using root access.
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
insert into secure (name, value) values ('lockscreen.disabled', 1);
or depending on version
sqlite3 /data/system/locksettings.db
insert into locksettings (name, value) values ('lockscreen.disabled', 1);

Is there a way to tell if the soft-keyboard is shown?

is there a way to tell if the softkeyboard is shown in an activity or not?
I tried
InputMethodManager manager = (InputMethodManager)
getSystemService(getApplicationContext().INPUT_METHOD_SERVICE);
manager.isActive(v)
but isActive returns false only before the first time the keyboard is shown, but if the kb appears and then dismissed, isActive returns true also.
so is there any other method to check for this issue.
thanks
According to this POST
You cannot detect if soft keyboard is shown or not, but you can indirectly know that a soft key board is shown by knowing that the View of your activity is resized.
Imagine you have a ListView and at the bottom an EditText, you want to go to the bottom of the list when a soft keyboard is shown after user clicks the EditText.
You need to implement a subclass of ListView, then use it in your ListActivity or Activity or View.
public class ThreadView extends ListView {
public ThreadView(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
#Override
protected void onSizeChanged(int xNew, int yNew, int xOld, int yOld) {
super.onSizeChanged(xNew, yNew, xOld, yOld);
if (yOld > yNew) {
setSelection(((ListAdapter) getAdapter()).getCount() - 1);
}
}
}
Hope this helps
PS. "check Configuration Changes" only works for hand keyboard.
You can detect the state AND coordinates of the software keyboard, using dumpsys shell command.
Because dumpsys requires permission.android.DUMP, which is a system application permission, you have two options: 1. use a rooted device to grant this permission. 2. override the problem using adb as described in my other answer.
Now, run the following command: dumpsys window InputMethod | grep "mHasSurface" to get the data you are looking for.
This is my idea. It is untested.
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks whether a keyboard is available which is not hard keyboard
if ((newConfig.keyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO)&&(newConfig.keyboardHidden != Configuration.KEYBOARDHIDDEN_NO)) {
Toast.makeText(this, "soft keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.keyboardHidden == Configuration.KEYBOARDHIDDEN_YES) {
Toast.makeText(this, "soft keyboard hidden", Toast.LENGTH_SHORT).show();
}
}
Please check Configuration Changes for your Activity
This for your AndroidManifest.xml
and this for your Activity class http://developer.android.com/reference/android/app/Activity.html#onConfigurationChanged(android.content.res.Configuration)
You will need to #Override the public method onConfigurationChanged(android.content.res.Configuration) of your Activity to be able to handle, for example, these values:
hardKeyboardHidden,
keyboard,
keyboardHidden
For all possible values check http://developer.android.com/reference/android/content/res/Configuration.html
You will see there something like this:
HARDKEYBOARDHIDDEN_NO
HARDKEYBOARDHIDDEN_UNDEFINED
HARDKEYBOARDHIDDEN_YES
KEYBOARDHIDDEN_NO
KEYBOARDHIDDEN_UNDEFINED
KEYBOARDHIDDEN_YES
KEYBOARD_12KEY
KEYBOARD_NOKEYS
KEYBOARD_QWERTY
KEYBOARD_UNDEFINED
Also there you will be able to read something like this:
public int hardKeyboardHidden // A flag indicating whether the hard keyboard
// has been hidden.
public int keyboard The kind of keyboard attached to the device.
public int keyboardHidden A flag indicating whether any keyboard is available.
UPDATE:
Here is a specific example of what I´m talking about:
http://developer.android.com/guide/topics/resources/runtime-changes.html
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Checks the orientation of the screen
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Toast.makeText(this, "landscape", Toast.LENGTH_SHORT).show();
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Toast.makeText(this, "portrait", Toast.LENGTH_SHORT).show();
}
// Checks whether a hardware keyboard is available
if (newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
Toast.makeText(this, "keyboard visible", Toast.LENGTH_SHORT).show();
} else if (newConfig.hardKeyboardHidden ==
Configuration.HARDKEYBOARDHIDDEN_YES) {
Toast.makeText(this, "keyboard hidden", Toast.LENGTH_SHORT).show();
}
}

Categories

Resources