package guard.api.sensitive.ui.sapiguard.hookedapis;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import guard.api.sensitive.ui.sapiguard.XHook;
import guard.api.sensitive.ui.sapiguard.utils.PrivacyManager;
import guard.api.sensitive.ui.sapiguard.utils.XParam;

public class XActivity extends XHook {
    private Methods mMethod;
    private String mActionName;

    private XActivity(Methods method, String restrictionName, String actionName) {
        super(restrictionName, method.name(), actionName);
        mMethod = method;
        mActionName = actionName;
    }

    public String getClassName() {
        return "android.app.Activity";
    }

    // @formatter:off

    // public Object getSystemService(String name)
    // public void startActivities(Intent[] intents)
    // public void startActivities(Intent[] intents, Bundle options)
    // public void startActivity(Intent intent)
    // public void startActivity(Intent intent, Bundle options)
    // public void startActivityForResult(Intent intent, int requestCode)
    // public void startActivityForResult(Intent intent, int requestCode, Bundle options)
    // public void startActivityFromChild(Activity child, Intent intent, int requestCode)
    // public void startActivityFromChild(Activity child, Intent intent, int requestCode, Bundle options)
    // public void startActivityFromFragment(Fragment fragment, Intent intent, int requestCode)
    // public void startActivityFromFragment(Fragment fragment, Intent intent, int requestCode, Bundle options)
    // public boolean startActivityIfNeeded(Intent intent, int requestCode)
    // public boolean startActivityIfNeeded(Intent intent, int requestCode, Bundle options)
    // public boolean startNextMatchingActivity(Intent intent)
    // public boolean startNextMatchingActivity(Intent intent, Bundle options)
    // frameworks/base/core/java/android/app/Activity.java

    // @formatter:on

    // @formatter:off
    private enum Methods {
        getSystemService,
        startActivities, startActivity, startActivityForResult, startActivityFromChild, startActivityFromFragment, startActivityIfNeeded, startNextMatchingActivity
    };
    // @formatter:on

    @SuppressLint("InlinedApi")
    public static List<XHook> getInstances() {
        List<XHook> listHook = new ArrayList<XHook>();
        listHook.add(new XActivity(Methods.getSystemService, null, null));

        List<Methods> startMethods = new ArrayList<Methods>(Arrays.asList(Methods.values()));
        startMethods.remove(Methods.getSystemService);

        // Intent send: browser
        for (Methods activity : startMethods)
            listHook.add(new XActivity(activity, PrivacyManager.cView, Intent.ACTION_VIEW));

        // Intent send: call/dial
        for (Methods activity : startMethods) {
            listHook.add(new XActivity(activity, PrivacyManager.cCalling, Intent.ACTION_CALL));
            listHook.add(new XActivity(activity, PrivacyManager.cCalling, Intent.ACTION_DIAL));
        }

        // Intent send: media
        for (Methods activity : startMethods) {
            listHook.add(new XActivity(activity, PrivacyManager.cMedia, MediaStore.ACTION_IMAGE_CAPTURE));
            listHook.add(new XActivity(activity, PrivacyManager.cMedia, MediaStore.ACTION_IMAGE_CAPTURE_SECURE));
            listHook.add(new XActivity(activity, PrivacyManager.cMedia, MediaStore.ACTION_VIDEO_CAPTURE));
        }

        return listHook;
    }

    @Override
    protected void before(XParam param) throws Throwable {

    }

    @Override
    protected void after(XParam param) throws Throwable {
        Log.d("jie-XActivity-", "after");



    }

//    private void getScreenShot(Activity aty) {
//        final Activity topAty = aty;
//        final String shotName = topAty.getComponentName().getClassName();
//        Log.d("jie-XActivity",shotName );
//        Thread screenshotT = new Thread(new Runnable() {
//            @Override
//            public void run() {
//                String screenshotName = Environment.getExternalStorageDirectory() + File.separator +"sapiguardSS" + File.separator  +shotName + ".png";
//                Log.d("jie-XActivity",screenshotName );
//
//                File f = new File(screenshotName);
//                if (f.exists())
//                    return;
//                try {
//                    // create bitmap screen capture
//                    View v1 = topAty.getWindow().getDecorView().getRootView();
//                    v1.setDrawingCacheEnabled(true);
//                    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
//                    v1.setDrawingCacheEnabled(false);
//                    File imageFile = new File(screenshotName);
//
//                    FileOutputStream outputStream = new FileOutputStream(imageFile);
//                    int quality = 100;
//                    bitmap.compress(Bitmap.CompressFormat.PNG, quality, outputStream);
//                    outputStream.flush();
//                    outputStream.close();
//
//                } catch (Throwable e) {
//                    e.printStackTrace();
//                }
//            }
//        });
//
//        screenshotT.start();
//    }
}
