I have device tracker application, where user needs to login so that devices is assigned to it.
But, now I want user should not able to use that devices until user have authenticated and should be available until user sing-out from that device ?
I want to retrieve password entered by user when he unlocks the screen and want to use it for my application authentication it's self and if password do not match I will unlock screen again.
I'm trying following code but still not basically able to lock and unlock screens using password.
public class AdminSettingsActivity extends Activity {
private EditText baseURLEditText;
private Button saveBaseURL;
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
saveBaseURL = (Button) findViewById(R.id.saveBaseURLBtn);
baseURLEditText = (EditText) findViewById(R.id.base_url_edit);
tv = (TextView) findViewById(R.id.text_admin_message);
tv.append(" " + DeviceTrackerApp.DEFAULT_BASE_URL);
saveBaseURL.setOnClickListener(new OnClickListener() {
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
String url = baseURLEditText.getText().toString().trim();
if (!url.isEmpty()) {
try {
if (URLUtil.isValidUrl(url)) {
new ValidateServerURL(AdminSettingsActivity.this).execute(url);
} else {
showInvalidURLToast();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
});
}
protected void showInvalidURLToast() {
// get your custom_toast.xml ayout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image_invalid_pwd);
image.setImageResource(R.drawable.invalid_pwd);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text_invalid_password);
text.setText("Invalid URL! Can't Save.");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
public void setURLStatus(int statusCode) {
if (statusCode == 200) {
DeviceTrackerApp.setBaseURL(baseURLEditText.getText().toString());
Toast.makeText(getApplicationContext(), "Valid URL Saved Successful",
Toast.LENGTH_LONG).show();
tv.setText("\nYour Setting saved successfuly. Press back button to go home screen\n");
} else {
showInvalidURLToast();
baseURLEditText.setText("");
}
}
}
and My DeviceTrackerAdmin class is
public class DeviceTrackerAdmin extends DeviceAdminReceiver {
static SharedPreferences getSamplePreferences(Context context) {
return context.getSharedPreferences(DeviceAdminReceiver.class.getName(), 0);
}
static String PREF_PASSWORD_QUALITY = "password_quality";
static String PREF_PASSWORD_LENGTH = "password_length";
static String PREF_MAX_FAILED_PW = "max_failed_pw";
void showToast(Context context, CharSequence msg) {
Toast.makeText(context, msg, Toast.LENGTH_SHORT).show();
}
@Override
public void onEnabled(Context context, Intent intent) {
showToast(context, "Sample Device Admin: enabled");
}
@Override
public CharSequence onDisableRequested(Context context, Intent intent) {
return "This is an optional message to warn the user about disabling.";
}
@Override
public void onDisabled(Context context, Intent intent) {
showToast(context, "Sample Device Admin: disabled");
}
@Override
public void onPasswordChanged(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw changed");
}
@Override
public void onPasswordFailed(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw failed");
}
@Override
public void onPasswordSucceeded(Context context, Intent intent) {
showToast(context, "Sample Device Admin: pw succeeded");
}
}
I also defined reciever in AndroidMenifest.xml
<receiver
android:name=".DeviceTrackerAdmin"
android:permission="android.permission.BIND_DEVICE_ADMIN" >
<meta-data
android:name="com.abc.devicetracker.DeviceTrackerApp"
android:resource="@layout/policies" >
<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" >
</action>
</intent-filter>
</meta-data>
</receiver>
But, every time onActivityResult()
on
`if (resultCode == RESULT_OK)` **OR** `if (resultCode == Activity.RESULT_OK)` condition fails as `resultCode is 0 only` So for Device locking I need to root my mobile.