package com.grarak.kerneladiutor.services;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;

import com.grarak.kerneladiutor.MainActivity;
import com.grarak.kerneladiutor.utils.Utils;
import com.grarak.kerneladiutor.utils.database.ProfileDB;
import com.grarak.kerneladiutor.utils.tools.Per_App;
import com.kerneladiutor.library.root.RootUtils;

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

/**
 * Created by joe on 3/2/16.
 */
public class PerAppMonitor extends AccessibilityService {
    public static String sPackageName;
    public static String accessibilityId;
    String last_package = "";
    String last_profile = "";
    long time= System.currentTimeMillis();

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
        AccessibilityServiceInfo serviceInfo = this.getServiceInfo();
        accessibilityId = serviceInfo.getId();

        PackageManager localPackageManager = getPackageManager();
        Intent intent = new Intent("android.intent.action.MAIN");
        intent.addCategory("android.intent.category.HOME");
        String launcher = localPackageManager.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName;

        if(event.getEventType() == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED) {
            sPackageName = event.getPackageName().toString();
            if ((System.currentTimeMillis() - time) < 2000) {
                if (!sPackageName.equals(launcher) || !sPackageName.equals("com.android.systemui")) {
                    process_window_change(sPackageName);
                }
            }
            else if ((System.currentTimeMillis() - time) >= 2000) {
                process_window_change(sPackageName);
            }
        }
    }

    @Override
    public void onInterrupt() {

    }

    private void process_window_change (String windowname) {
        if (!windowname.equals("com.android.systemui")) {

            if (!Per_App.app_profile_exists(windowname, getApplicationContext())) {
                windowname = "Default";
            }
            if (Per_App.app_profile_exists(windowname, getApplicationContext())) {
                ArrayList<String> info = new ArrayList<String>();
                // Item 0 is packagename Item 1 is the profile ID
                info = Per_App.app_profile_info(windowname, getApplicationContext());

                if (!windowname.equals(last_package) && !info.get(1).equals(last_profile)) {
                    last_package = windowname;
                    last_profile = info.get(1);
                    time = System.currentTimeMillis();
                    ProfileDB profileDB = new ProfileDB(getApplicationContext());
                    final List<ProfileDB.ProfileItem> profileItems = profileDB.getAllProfiles();

                    for (int i = 0; i < profileItems.size(); i++) {
                        if (profileItems.get(i).getID().equals(info.get(1))) {
                            if (Utils.getBoolean("Per_App_Toast", false, MainActivity.context)) {
                                Utils.toast("Applying Profile: " + profileItems.get(i).getName(), MainActivity.context);
                            }
                            Log.i("Kernel Adiutor", "Applying Profile:  " + profileItems.get(i).getName() + " for package " + windowname);
                            ProfileDB.ProfileItem profileItem = profileItems.get(i);
                            List<String> paths = profileItem.getPath();
                            for (int x = 0; x < paths.size(); x++) {
                                RootUtils.runCommand(profileItem.getCommands().get(x));
                            }
                        }
                    }
                }
            }
        }
    }

}

@@ -1614,17 +1614,17 @@
 time) %3C 
-2
+1
 000) %7B%0A 
@@ -1873,17 +1873,17 @@
 ime) %3E= 
-2
+1
 000) %7B%0A 
@@ -2084,71 +2084,8 @@
 ) %7B%0A
-        if (!windowname.equals(%22com.android.systemui%22)) %7B%0A%0A    
     
@@ -2172,20 +2172,16 @@
         
-    
 windowna
@@ -2200,34 +2200,26 @@
 t%22;%0A        
-  
-  %7D%0A    
+%7D%0A
         if (
@@ -2297,20 +2297,16 @@
         
-    
 ArrayLis
@@ -2359,20 +2359,16 @@
         
- 
-   
 // Item 
@@ -2409,20 +2409,16 @@
 file ID%0A
-    
         
@@ -2488,28 +2488,24 @@
 ontext());%0A%0A
-    
             
@@ -2593,28 +2593,24 @@
             
-    
 last_package
@@ -2624,20 +2624,16 @@
 owname;%0A
-    
         
@@ -2668,20 +2668,16 @@
 get(1);%0A
-    
         
@@ -2727,36 +2727,32 @@
                 
-    
 ProfileDB profil
@@ -2793,28 +2793,24 @@
 Context());%0A
-    
             
@@ -2899,36 +2899,32 @@
                 
-   
- 
 for (int i = 0; 
@@ -2951,28 +2951,24 @@
 e(); i++) %7B%0A
-    
             
@@ -3050,28 +3050,24 @@
             
- 
-   
 if (Utils.ge
@@ -3152,20 +3152,16 @@
         
-  
-  
 Utils.to
@@ -3261,34 +3261,26 @@
             
-    %7D%0A    
+%7D%0A
             
@@ -3418,36 +3418,32 @@
                 
- 
-   
 ProfileDB.Profil
@@ -3507,20 +3507,16 @@
         
-   
- 
 List%3CStr
@@ -3567,36 +3567,32 @@
                 
-    
 for (int x = 0; 
@@ -3644,20 +3644,16 @@
         
- 
-   
 RootUtil
@@ -3704,38 +3704,8 @@
 ));%0A
-                            %7D%0A
     

Change time to 1 sec for app checking