package cn.libery.snapshot;

import android.content.Context;
import android.content.Intent;
import android.database.ContentObserver;
import android.database.Cursor;
import android.net.Uri;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;


/**
 * Created by Libery on 2017/7/17.
 * Email:libery.szq@qq.com
 */

public class SnapShotListenerManager {

    private Context mContext;
    public static final String TAG = "SnapShotListenerManager";

    private static final String[] KEYWORDS = {
            "screenshot", "screenshots", "screen_shot", "screen-shot", "screen shot",
            "screencapture", "screen_capture", "screen-capture", "screen capture",
            "screencap", "screen_cap", "screen-cap", "screen cap", "截屏"
    };

    /**
     * 读取媒体数据库时需要读取的列
     */
    private static final String[] MEDIA_PROJECTIONS = {
            MediaStore.Images.ImageColumns.DATA,
            MediaStore.Images.ImageColumns.DATE_TAKEN,
    };

    /**
     * 内部存储器内容观察者
     */
    private ContentObserver mInternalObserver;

    /**
     * 外部存储器内容观察者
     */
    private ContentObserver mExternalObserver;

    public SnapShotListenerManager(final Context context) {
        mContext = context;
        initManager();
    }

    /**
     * 初始化
     */
    private void initManager() {
        if (mContext == null) {
            return;
        }
        final Handler handler = new Handler(mContext.getMainLooper());
        mInternalObserver = new MediaContentObserver(MediaStore.Images.Media.INTERNAL_CONTENT_URI, handler);
        mExternalObserver = new MediaContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, handler);
    }

    /**
     * 添加监听
     */
    public void startListener() {
        if (mContext == null) {
            return;
        }
        mContext.getContentResolver().registerContentObserver(
                MediaStore.Images.Media.INTERNAL_CONTENT_URI,
                false,
                mInternalObserver
        );
        mContext.getContentResolver().registerContentObserver(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                false,
                mExternalObserver
        );
    }

    /**
     * 注销监听
     */
    public void stopListener() {
        if (mContext == null) {
            return;
        }
        mContext.getContentResolver().unregisterContentObserver(mInternalObserver);
        mContext.getContentResolver().unregisterContentObserver(mExternalObserver);
    }

    private void handleMediaContentChange(Uri contentUri) {
        Cursor cursor = null;
        try {
            // 数据改变时查询数据库中最后加入的一条数据
            cursor = mContext.getContentResolver().query(
                    contentUri,
                    MEDIA_PROJECTIONS,
                    null,
                    null,
                    MediaStore.Images.ImageColumns.DATE_ADDED + " desc limit 1"
            );

            if (cursor == null) {
                return;
            }
            if (!cursor.moveToFirst()) {
                return;
            }

            // 获取各列的索引
            int dataIndex = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            int dataData = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATE_TAKEN);
            // 获取行数据
            String data = cursor.getString(dataIndex);
            long dateTime = cursor.getLong(dataData);
            // 处理获取到的第一行数据
            if (checkTime(dateTime)) {
                handleMediaRowData(data);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null && !cursor.isClosed()) {
                cursor.close();
            }
        }
    }

    /**
     * 检查是否大于当前时间两秒，是则舍弃，反之亦然
     *
     * @param dateTime 图片保存时间
     * @return true 符合预期
     */
    private boolean checkTime(final long dateTime) {
        return System.currentTimeMillis() - dateTime < 2 * 1000;
    }

    private String originPath;

    /**
     * 处理监听到的资源
     */
    private void handleMediaRowData(String data) {
        if (checkScreenShot(data)) {
            if (data.equals(originPath)) {
                return;
            }
            originPath = data;

            Log.d(TAG, data);
            Intent intent = new Intent(mContext, SnapShotActivity.class);
            intent.putExtra("snapshot_path", data);
            mContext.startActivity(intent);
        } else {
            Log.d(TAG, "Not screenshot event:" + data);
        }
    }

    /**
     * 判断是否是截屏
     */
    private boolean checkScreenShot(String data) {
        data = data.toLowerCase();
        // 判断图片路径是否含有指定的关键字之一, 如果有, 则认为当前截屏了
        for (String keyWork : KEYWORDS) {
            if (data.contains(keyWork)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 媒体内容观察者(观察媒体数据库的改变)
     */
    private class MediaContentObserver extends ContentObserver {

        private Uri mContentUri;

        MediaContentObserver(Uri contentUri, Handler handler) {
            super(handler);
            mContentUri = contentUri;
        }

        @Override
        public void onChange(boolean selfChange) {
            super.onChange(selfChange);
            Log.d(TAG, mContentUri.toString());
            handleMediaContentChange(mContentUri);
        }
    }

}

@@ -3754,10 +3754,16 @@
 %E5%BD%93%E5%89%8D%E6%97%B6%E9%97%B4
-%E4%B8%A4%E7%A7%92
+%E4%BA%94%E7%A7%92(%E5%85%BC%E5%AE%B9%E5%B0%8F%E7%B1%B3)
 %EF%BC%8C%E6%98%AF%E5%88%99%E8%88%8D
@@ -3947,17 +3947,17 @@
 eTime %3C 
-2
+5
  * 1000;

fix snapScreen