Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug fix and lock screen feature added #57

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<receiver
android:name=".Admin"
android:permission="android.permission.BIND_DEVICE_ADMIN">
<meta-data
android:name="android.app.device_admin"
android:resource="@xml/policies" />

<intent-filter>
<action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
</intent-filter>

</receiver>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.beautycoder.applicationlockscreenexample;

import android.app.admin.DeviceAdminReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Admin extends DeviceAdminReceiver {
@Override
public void onEnabled(Context context, Intent intent) {
Toast.makeText(context, "Enabled Permission", Toast.LENGTH_SHORT).show();
super.onEnabled(context, intent);
}

@Override
public void onDisabled(Context context, Intent intent) {
Toast.makeText(context, "Disabled Permission", Toast.LENGTH_SHORT).show();
super.onDisabled(context, intent);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public void onFingerprintSuccessful() {
@Override
public void onPinLoginFailed() {
Toast.makeText(MainActivity.this, "Pin failed", Toast.LENGTH_SHORT).show();
PFCodeView pfCodeView;
pfCodeView = findViewById(R.id.code_view);
pfCodeView.clearCode();
}

@Override
Expand Down Expand Up @@ -120,5 +123,11 @@ private void showMainFragment() {
.replace(R.id.container_view, fragment).commit();
}


}
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onBackPressed() {
super.onBackPressed();
finishAffinity();
finish();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,71 @@
*/

public class MainFragment extends Fragment {
ComponentName componentName;
DevicePolicyManager devicePolicyManager;
Button lock, enable;
public static final int RESULT_ENABLE = 11;
private ActivityManager activityManager;


@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_main, container, false);

devicePolicyManager = (DevicePolicyManager) getActivity().getSystemService(Context.DEVICE_POLICY_SERVICE);
activityManager = (ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE);
componentName = new ComponentName(getActivity(), Admin.class);

lock = view.findViewById(R.id.lock);
enable = view.findViewById(R.id.enable);

lock.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
boolean active = devicePolicyManager.isAdminActive(componentName);
if (active) {
startActivity(new Intent(getContext(), MainActivity.class));
devicePolicyManager.lockNow();
} else {
Toast.makeText(getContext(), "You need to enable the Admin Device Features", Toast.LENGTH_SHORT).show();
}
}
});

enable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "Required Permission");
startActivityForResult(intent, RESULT_ENABLE);
}
});
return view;
}

@Override
public void onResume() {
super.onResume();
boolean isActive = devicePolicyManager.isAdminActive(componentName);
enable.setVisibility(isActive ? View.GONE : View.VISIBLE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RESULT_ENABLE:
if (resultCode == getActivity().RESULT_OK) {
Toast.makeText(getContext(), "You have enabled the Admin Device features", Toast.LENGTH_SHORT).show();
} else if (resultCode == getActivity().RESULT_FIRST_USER) {
Toast.makeText(getContext(), "Enabled Permission", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getContext(), "Problem to enable the Admin Device features", Toast.LENGTH_SHORT).show();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
}
37 changes: 31 additions & 6 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,37 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:layout_height="match_parent"
<RelativeLayout
android:layout_width="match_parent"
android:text="You logged In!!!"
android:gravity="center"/>
android:layout_height="match_parent">

<TextView
android:id="@+id/tvLoggedIn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="You logged In!!!" />

<Button
android:id="@+id/lock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvLoggedIn"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="lock" />

<Button
android:id="@+id/enable"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/lock"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="Enable" />
</RelativeLayout>
</FrameLayout>
Empty file.