// Path: app/src/main/java/com/vv/androidreview/base/system/AppManager.java
// public class AppManager {
// 
//     private static Stack<Activity> activityStack;
//     private static AppManager instance;
// 
//     private AppManager() {}
// 
//     /**
//      * 单一实例
//      */
//     public static AppManager getAppManager() {
//         if (instance == null) {
//             instance = new AppManager();
//         }
//         return instance;
//     }
// 
//     /**
//      * 添加Activity到堆栈
//      */
//     public void addActivity(Activity activity) {
//         if (activityStack == null) {
//             activityStack = new Stack<Activity>();
//         }
//         activityStack.add(activity);
//     }
// 
//     /**
//      * 获取当前Activity（堆栈中最后一个压入的）
//      */
//     public Activity currentActivity() {
//         Activity activity = activityStack.lastElement();
//         return activity;
//     }
// 
//     /**
//      * 结束当前Activity（堆栈中最后一个压入的）
//      */
//     public void finishActivity() {
//         Activity activity = activityStack.lastElement();
//         finishActivity(activity);
//     }
// 
//     /**
//      * 结束指定的Activity
//      */
//     public void finishActivity(Activity activity) {
//         if (activity != null && !activity.isFinishing()) {
//             activityStack.remove(activity);
//             activity.finish();
//             activity = null;
//         }
//     }
// 
//     /**
//      * 结束指定类名的Activity
//      */
//     public void finishActivity(Class<?> cls) {
//         for (Activity activity : activityStack) {
//             if (activity.getClass().equals(cls)) {
//                 finishActivity(activity);
//                 break;
//             }
//         }
//     }
// 
//     /**
//      * 结束所有Activity
//      */
//     public void finishAllActivity() {
//         for (int i = 0, size = activityStack.size(); i < size; i++) {
//             if (null != activityStack.get(i)) {
//                 finishActivity(activityStack.get(i));
//                 break;
//             }
//         }
//         activityStack.clear();
//     }
// 
//     /**
//      * 获取指定的Activity
//      *
//      * @author kymjs
//      */
//     public static Activity getActivity(Class<?> cls) {
//         if (activityStack != null)
//             for (Activity activity : activityStack) {
//                 if (activity.getClass().equals(cls)) {
//                     return activity;
//                 }
//             }
//         return null;
//     }
// 
//     public static Stack<Activity> getActivitys(){
//         return activityStack;
//     }
// 
//     /**
//      * 退出应用程序
//      */
//     public void AppExit(Context context) {
//         try {
//             finishAllActivity();
//             // 杀死该应用进程
//             android.os.Process.killProcess(android.os.Process.myPid());
//             System.exit(0);
//         } catch (Exception e) {
//         }
//     }
// 
//     /**返回当前Activity栈中Activity的数量
//      * @return
//      */
//     public int getActivityCount(){
//         int count = activityStack.size();
//         return count;
//     }
// 
//     /**
//      * 堆栈中移除Activity
//      */
//     public void removeActivity(Activity activity) {
//         if (activityStack == null) {
//            return;
//         }else if(activityStack.contains(activity)){
//             activityStack.remove(activity);
//         }
// 
//         if (activity != null && !activity.isFinishing()) {
//             activity.finish();
//             activity = null;
//         }
//     }
// }


import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.widget.Toast;
import com.vv.androidreview.R;
import com.vv.androidreview.base.system.AppManager;
/*
 * Copyright (c) 2016. Vv <envyfan@qq.com><http://www.v-sounds.com/>
 *
 * This file is part of AndroidReview (Android面试复习)
 *
 * AndroidReview is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  AndroidReview is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 * along with AndroidReview.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.vv.androidreview.utils;




/**
 * Author：Vv on 2015/7/21 15:30
 * Mail：envyfan@qq.com
 * Description：双击退出
 */
public class DoubleClickExitHelper {
    private final Activity mActivity;

    private boolean isOnKeyBacking;
    private Handler mHandler;
    private Toast mBackToast;

    public DoubleClickExitHelper(Activity activity) {
        mActivity = activity;
        mHandler = new Handler(Looper.getMainLooper());
    }

    /**
     * Activity onKeyDown事件
     * */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode != KeyEvent.KEYCODE_BACK) {
            return false;
        }
        if(isOnKeyBacking) {
            mHandler.removeCallbacks(onBackTimeRunnable);
            if(mBackToast != null){
                mBackToast.cancel();
            }
            // �?�?
// Path: app/src/main/java/com/vv/androidreview/base/system/AppManager.java
// public class AppManager {
// 
//     private static Stack<Activity> activityStack;
//     private static AppManager instance;
// 
//     private AppManager() {}
// 
//     /**
//      * 单一实例
//      */
//     public static AppManager getAppManager() {
//         if (instance == null) {
//             instance = new AppManager();
//         }
//         return instance;
//     }
// 
//     /**
//      * 添加Activity到堆栈
//      */
//     public void addActivity(Activity activity) {
//         if (activityStack == null) {
//             activityStack = new Stack<Activity>();
//         }
//         activityStack.add(activity);
//     }
// 
//     /**
//      * 获取当前Activity（堆栈中最后一个压入的）
//      */
//     public Activity currentActivity() {
//         Activity activity = activityStack.lastElement();
//         return activity;
//     }
// 
//     /**
//      * 结束当前Activity（堆栈中最后一个压入的）
//      */
//     public void finishActivity() {
//         Activity activity = activityStack.lastElement();
//         finishActivity(activity);
//     }
// 
//     /**
//      * 结束指定的Activity
//      */
//     public void finishActivity(Activity activity) {
//         if (activity != null && !activity.isFinishing()) {
//             activityStack.remove(activity);
//             activity.finish();
//             activity = null;
//         }
//     }
// 
//     /**
//      * 结束指定类名的Activity
//      */
//     public void finishActivity(Class<?> cls) {
//         for (Activity activity : activityStack) {
//             if (activity.getClass().equals(cls)) {
//                 finishActivity(activity);
//                 break;
//             }
//         }
//     }
// 
//     /**
//      * 结束所有Activity
//      */
//     public void finishAllActivity() {
//         for (int i = 0, size = activityStack.size(); i < size; i++) {
//             if (null != activityStack.get(i)) {
//                 finishActivity(activityStack.get(i));
//                 break;
//             }
//         }
//         activityStack.clear();
//     }
// 
//     /**
//      * 获取指定的Activity
//      *
//      * @author kymjs
//      */
//     public static Activity getActivity(Class<?> cls) {
//         if (activityStack != null)
//             for (Activity activity : activityStack) {
//                 if (activity.getClass().equals(cls)) {
//                     return activity;
//                 }
//             }
//         return null;
//     }
// 
//     public static Stack<Activity> getActivitys(){
//         return activityStack;
//     }
// 
//     /**
//      * 退出应用程序
//      */
//     public void AppExit(Context context) {
//         try {
//             finishAllActivity();
//             // 杀死该应用进程
//             android.os.Process.killProcess(android.os.Process.myPid());
//             System.exit(0);
//         } catch (Exception e) {
//         }
//     }
// 
//     /**返回当前Activity栈中Activity的数量
//      * @return
//      */
//     public int getActivityCount(){
//         int count = activityStack.size();
//         return count;
//     }
// 
//     /**
//      * 堆栈中移除Activity
//      */
//     public void removeActivity(Activity activity) {
//         if (activityStack == null) {
//            return;
//         }else if(activityStack.contains(activity)){
//             activityStack.remove(activity);
//         }
// 
//         if (activity != null && !activity.isFinishing()) {
//             activity.finish();
//             activity = null;
//         }
//     }
// }


// Path: app/src/main/java/com/vv/androidreview/utils/DoubleClickExitHelper.java
import android.app.Activity;
import android.os.Handler;
import android.os.Looper;
import android.view.KeyEvent;
import android.widget.Toast;
import com.vv.androidreview.R;
import com.vv.androidreview.base.system.AppManager;

/*
 * Copyright (c) 2016. Vv <envyfan@qq.com><http://www.v-sounds.com/>
 *
 * This file is part of AndroidReview (Android面试复习)
 *
 * AndroidReview is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 *  AndroidReview is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 * along with AndroidReview.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.vv.androidreview.utils;




/**
 * Author：Vv on 2015/7/21 15:30
 * Mail：envyfan@qq.com
 * Description：双击退出
 */
public class DoubleClickExitHelper {
    private final Activity mActivity;

    private boolean isOnKeyBacking;
    private Handler mHandler;
    private Toast mBackToast;

    public DoubleClickExitHelper(Activity activity) {
        mActivity = activity;
        mHandler = new Handler(Looper.getMainLooper());
    }

    /**
     * Activity onKeyDown事件
     * */
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode != KeyEvent.KEYCODE_BACK) {
            return false;
        }
        if(isOnKeyBacking) {
            mHandler.removeCallbacks(onBackTimeRunnable);
            if(mBackToast != null){
                mBackToast.cancel();
            }
            // �?�?
            AppManager.getAppManager().AppExit(mActivity);