恢复最小化或终止的应用程序时要求启用 pin

Ask for enabled pin when resume minimized or killed app

我在设置中添加了自定义 Pin 图。我希望应用程序被终止时 and/or 最小化要求输入密码(不是用户的用户名和密码,而是我在应用程序中输入的自定义密码)

    package com.musala.imds.fragments;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceCategory;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.PreferenceManager;
import com.musala.imds.utils.SessionUtils;
import android.support.v7.preference.SwitchPreferenceCompat;
import android.widget.CompoundButton;
import android.widget.Toast;

import com.github.orangegangsters.lollipin.lib.managers.AppLock;
import com.musala.imds.R;
import com.musala.imds.activities.CustomPinActivity;
import com.musala.imds.model.Employee;
import com.musala.imds.utils.MDSConstants;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.realm.Realm;

/**
 * Created by lyubomir.angelov on 19.7.2016 г..
 * Preference fragment
 */
public class SettingsFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceChangeListener {
    private static final int REQUEST_PIN_CODE_ENABLE = 100;
    private static final int REQUEST_PIN_CODE_CHECK = 101;

    private SharedPreferences prefs;

    private PreferenceCategory mCategory;
    private Preference changePin;
    private SwitchPreferenceCompat enableOrDisablePin;

    @Inject
    protected SessionUtils sessionUtils;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());

        // Load the preferences from an XML resource
        mCategory = (PreferenceCategory) findPreference(MDSConstants.SETTINGS.PREF_CATEGORY_KEY_PIN);
        changePin = findPreference(MDSConstants.SETTINGS.PREF_KEY_CHANGE_PIN);
        enableOrDisablePin = (SwitchPreferenceCompat) findPreference(MDSConstants.SETTINGS.PREF_KEY_PIN);

        enableOrDisablePin.setOnPreferenceChangeListener(this);

        if (!enableOrDisablePin.isChecked()) {
            mCategory.removePreference(changePin);
        }
    }

    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        setPreferencesFromResource(R.xml.preferences, rootKey);
    }

    @Override
    public boolean onPreferenceTreeClick(Preference preference) {
        if (preference.getKey().equals(MDSConstants.SETTINGS.PREF_KEY_CHANGE_PIN)) {
            Intent intent = new Intent(getActivity(), CustomPinActivity.class);
            intent.putExtra(AppLock.EXTRA_TYPE, AppLock.CHANGE_PIN);
            startActivity(intent);
        }
        return super.onPreferenceTreeClick(preference);
    }

    @Override
    public boolean onPreferenceChange(Preference pref, Object newValue) {
        Intent intent = new Intent(getActivity(), CustomPinActivity.class);
        if (pref.getKey().equals(MDSConstants.SETTINGS.PREF_KEY_PIN)) {
            boolean currentlyChecked = !(Boolean) newValue;

            if (currentlyChecked) {
                mCategory.removePreference(changePin);

                Intent enterPinIntent = new Intent(getActivity(), CustomPinActivity.class);
                enterPinIntent.putExtra(AppLock.EXTRA_TYPE, AppLock.UNLOCK_PIN);
                startActivityForResult(enterPinIntent, REQUEST_PIN_CODE_CHECK);
            } else {
                intent.putExtra(AppLock.EXTRA_TYPE, AppLock.ENABLE_PINLOCK);
                startActivityForResult(intent, REQUEST_PIN_CODE_ENABLE);
            }
        }
        return true;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_PIN_CODE_ENABLE && resultCode == Activity.RESULT_OK) {
                Toast.makeText(getActivity(), "PinCode enabled", Toast.LENGTH_SHORT).show();
                mCategory.addPreference(changePin);
        } else if (requestCode == REQUEST_PIN_CODE_CHECK ) {
            if (resultCode != Activity.RESULT_OK) {
                enableOrDisablePin.setChecked(true);
                if (mCategory.findPreference(MDSConstants.SETTINGS.PREF_KEY_PIN) != null) {
                    mCategory.addPreference(changePin);
                }
            }
        }
    }

/*    private void setPinSwitchEnabled(boolean enabled){
        pinSwitch.setOnCheckedChangeListener(null);
        pinSwitch.setChecked(enabled);
        pinSwitch.setOnCheckedChangeListener(this);

        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(MDSConstants.SharedPreferences.PIN_ENABLED, enabled);
        editor.apply();}*/

}

我设法通过 BaseLifecycleHandler 最小化解决了问题

package com.musala.imds.utils;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;

import com.github.orangegangsters.lollipin.lib.managers.AppLock;
import com.musala.imds.MyApplication;
import com.musala.imds.activities.CustomPinActivity;
import com.musala.imds.activities.SplashScreenActivity;

import javax.inject.Inject;

/**
 * Created by aleksandar.milushev on 21.12.2016 г..
 */

public class BaseLifecycleHandler implements Application.ActivityLifecycleCallbacks {
    private static final String TAG = BaseLifecycleHandler.class.getName();

    @Inject
    Context context;
    private SharedPreferences prefs;

    private static int resumed;
    private static int paused;
    private static int started;
    private static int stopped;

    public BaseLifecycleHandler() {
        MyApplication.getObjectGraph().inject(this);
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
    }

    @Override
    public void onActivityDestroyed(Activity activity) {
    }

    @Override
    public void onActivityResumed(Activity activity) {
        ++resumed;

        if ((isPinCodeSet())
                && (isAppPreviousStateMinimized())
                && (activity instanceof CustomPinActivity) == false) {
            Log.d(TAG, "onActivityResumed and PIN required");
            Intent enterPinIntent = new Intent(activity, CustomPinActivity.class);
            enterPinIntent.putExtra(AppLock.EXTRA_TYPE, AppLock.UNLOCK_PIN);
            activity.startActivity(enterPinIntent);
        }
    }

    @Override
    public void onActivityPaused(Activity activity) {
        ++paused;

        if (activity instanceof SplashScreenActivity) {
            return;
        }
        setKeyAppPreviosStateMinimized(!isApplicationInForeground() && !isApplicationVisible());
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
    }

    @Override
    public void onActivityStarted(Activity activity) {
        ++started;
    }

    @Override
    public void onActivityStopped(Activity activity) {
        ++stopped;
        String classSimpleName = activity.getClass().getSimpleName();
        boolean isSplashScreen = classSimpleName.equals(SplashScreenActivity.class.getSimpleName());

        if (isSplashScreen) {
            return;
        }
        setKeyAppPreviosStateMinimized(!isApplicationInForeground() && !isApplicationVisible());
    }

    public static boolean isApplicationVisible() {
        return started > stopped;
    }

    public static boolean isApplicationInForeground() {
        return resumed > paused;
    }

    private void setKeyAppPreviosStateMinimized(boolean minimized){
        SharedPreferences.Editor editor = prefs.edit();
        editor.putBoolean(MDSConstants.SharedPreferences.APP_PREVIOUS_STATE_MINIMIZED, minimized);
        editor.commit();
    }

    private boolean isAppPreviousStateMinimized(){
        return prefs.getBoolean(MDSConstants.SharedPreferences.APP_PREVIOUS_STATE_MINIMIZED, false);
    }

    private boolean isPinCodeSet(){
        return prefs.getBoolean(MDSConstants.SETTINGS.PREF_KEY_PIN, false);
    }

}

但是我在kill app的时候还是遇到了问题