Advertisement
Guest User

Sieve AuthService

a guest
Mar 23rd, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.75 KB | None | 0 0
  1. package com.mwr.example.sieve;
  2.  
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.app.Service;
  6. import android.content.ContentValues;
  7. import android.content.CursorLoader;
  8. import android.content.Intent;
  9. import android.database.Cursor;
  10. import android.os.Bundle;
  11. import android.os.Handler;
  12. import android.os.HandlerThread;
  13. import android.os.IBinder;
  14. import android.os.Looper;
  15. import android.os.Message;
  16. import android.os.Messenger;
  17. import android.os.RemoteException;
  18. import android.support.v4.app.NotificationCompat;
  19. import android.util.Log;
  20.  
  21. public class AuthService extends Service {
  22.   static final int MSG_CHECK = 2354;
  23.  
  24.   static final int MSG_CHECK_IF_INITALISED = 2;
  25.  
  26.   static final int MSG_FIRST_LAUNCH = 4;
  27.  
  28.   static final int MSG_SAY_HELLO = 1;
  29.  
  30.   static final int MSG_SET = 6345;
  31.  
  32.   static final int MSG_UNREGISTER = -1;
  33.  
  34.   public static final String PASSWORD = "com.mwr.example.sieve.PASSWORD";
  35.  
  36.   public static final String PIN = "com.mwr.example.sieve.PIN";
  37.  
  38.   private static final String TAG = "m_AuthService";
  39.  
  40.   static final int TYPE_KEY = 7452;
  41.  
  42.   static final int TYPE_PIN = 9234;
  43.  
  44.   private int NOTIFICATION = 2131034112;
  45.  
  46.   private NotificationManager nManager;
  47.  
  48.   private Messenger responseHandler;
  49.  
  50.   private Messenger serviceHandler;
  51.  
  52.   private Looper serviceLooper;
  53.  
  54.   private String getKey() {
  55.     Cursor cursor = (new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "Password" }, null, null, null)).loadInBackground();
  56.     cursor.moveToFirst();
  57.     return cursor.getString(cursor.getColumnIndex("Password"));
  58.   }
  59.  
  60.   private boolean setKey(String paramString) {
  61.     ContentValues contentValues = new ContentValues();
  62.     contentValues.put("Password", paramString);
  63.     return (getContentResolver().insert(DBContentProvider.KEYS_URI, contentValues) != null);
  64.   }
  65.  
  66.   private boolean setPin(String paramString) {
  67.     ContentValues contentValues = new ContentValues();
  68.     contentValues.put("pin", paramString);
  69.     return (getContentResolver().update(DBContentProvider.KEYS_URI, contentValues, "pin IS NULL", null) > 0);
  70.   }
  71.  
  72.   private void showNotification() {
  73.     NotificationCompat.Builder builder = (new NotificationCompat.Builder(this)).setSmallIcon(2130837504).setContentTitle(getText(2131034112)).setOngoing(true).setContentText("Click to access your passwords");
  74.     Intent intent = new Intent(this, MainLoginActivity.class);
  75.     intent.addFlags(1048576);
  76.     builder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, intent, 268435456));
  77.     this.nManager.notify(this.NOTIFICATION, builder.build());
  78.   }
  79.  
  80.   private boolean verifyKey(String paramString) { return ((new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "Password" }, "Password = ?", new String[] { paramString }, null)).loadInBackground().getCount() == 1); }
  81.  
  82.   private boolean verifyPin(String paramString) { return ((new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "pin" }, "pin = ?", new String[] { paramString }, null)).loadInBackground().getCount() == 1); }
  83.  
  84.   public boolean checkKeyExists() { return ((new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "Password" }, null, null, null)).loadInBackground().getCount() > 0); }
  85.  
  86.   public boolean checkPinExists() {
  87.     Cursor cursor = (new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "pin" }, null, null, null)).loadInBackground();
  88.     boolean bool = false;
  89.     cursor.moveToFirst();
  90.     for (byte b = 0;; b++) {
  91.       if (b >= cursor.getCount())
  92.         return bool;
  93.       if (cursor.getString(cursor.getColumnIndex("pin")) != null) {
  94.         bool = true;
  95.       } else {
  96.         bool = false;
  97.       }
  98.     }
  99.   }
  100.  
  101.   public IBinder onBind(Intent paramIntent) { return this.serviceHandler.getBinder(); }
  102.  
  103.   public void onCreate() {
  104.     this.nManager = (NotificationManager)getSystemService("notification");
  105.     HandlerThread handlerThread = new HandlerThread("m_AuthService", 10);
  106.     handlerThread.start();
  107.     this.serviceLooper = handlerThread.getLooper();
  108.     this.serviceHandler = new Messenger(new MessageHandler(this, this.serviceLooper));
  109.   }
  110.  
  111.   public void onDestroy() { this.nManager.cancelAll(); }
  112.  
  113.   public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2) { return 1; }
  114.  
  115.   private final class MessageHandler extends Handler {
  116.     public MessageHandler(Looper param1Looper) { super(param1Looper); }
  117.    
  118.     private void sendResponseMessage(int param1Int1, int param1Int2, int param1Int3, Bundle param1Bundle) {
  119.       try {
  120.         Message message = Message.obtain(null, param1Int1, param1Int2, param1Int3);
  121.         if (param1Bundle != null)
  122.           message.setData(param1Bundle);
  123.         AuthService.this.responseHandler.send(message);
  124.         return;
  125.       } catch (RemoteException param1Bundle) {
  126.         Log.e("m_AuthService", "Unable to send message: " + param1Int1);
  127.         return;
  128.       }
  129.     }
  130.    
  131.     private void sendUnrecognisedMessage() {
  132.       try {
  133.         Message message = Message.obtain(null, 111111, 122222, 1, null);
  134.         AuthService.this.responseHandler.send(message);
  135.         return;
  136.       } catch (RemoteException remoteException) {
  137.         Log.e("m_AuthService", "Unable to send message");
  138.         return;
  139.       }
  140.     }
  141.    
  142.     public void handleMessage(Message param1Message) {
  143.       byte b2;
  144.       byte b1;
  145.       String str;
  146.       AuthService.this.responseHandler = param1Message.replyTo;
  147.       Bundle bundle = (Bundle)param1Message.obj;
  148.       switch (param1Message.what) {
  149.         default:
  150.           Log.e("m_AuthService", "Error: unrecognized command: " + param1Message.what);
  151.           sendUnrecognisedMessage();
  152.           super.handleMessage(param1Message);
  153.           return;
  154.         case 2354:
  155.           if (param1Message.arg1 == 7452) {
  156.             b2 = 42;
  157.             str = bundle.getString("com.mwr.example.sieve.PASSWORD");
  158.             if (AuthService.this.verifyKey(str)) {
  159.               AuthService.this.showNotification();
  160.               b1 = 0;
  161.               Bundle bundle1 = bundle;
  162.             } else {
  163.               b1 = 1;
  164.               str = bundle;
  165.             }
  166.           } else if (str.arg1 == 9234) {
  167.             b2 = 41;
  168.             String str1 = bundle.getString("com.mwr.example.sieve.PIN");
  169.             if (AuthService.this.verifyPin(str1)) {
  170.               Bundle bundle1 = new Bundle();
  171.               bundle1.putString("com.mwr.example.sieve.PASSWORD", AuthService.this.getKey());
  172.               b1 = 0;
  173.             } else {
  174.               b1 = 1;
  175.               str = bundle;
  176.             }
  177.           } else {
  178.             sendUnrecognisedMessage();
  179.             return;
  180.           }
  181.           sendResponseMessage(5, b2, b1, str);
  182.           return;
  183.         case 4:
  184.           if (AuthService.this.checkKeyExists()) {
  185.             if (AuthService.this.checkPinExists()) {
  186.               b1 = 31;
  187.             } else {
  188.               b1 = 32;
  189.             }
  190.           } else {
  191.             b1 = 33;
  192.           }
  193.           sendResponseMessage(3, b1, 1, null);
  194.           return;
  195.         case 6345:
  196.           break;
  197.       }
  198.       if (str.arg1 == 7452) {
  199.         b2 = 42;
  200.         str = bundle.getString("com.mwr.example.sieve.PASSWORD");
  201.         if (AuthService.this.setKey(str)) {
  202.           b1 = 0;
  203.         } else {
  204.           b1 = 1;
  205.         }
  206.       } else if (str.arg1 == 9234) {
  207.         b2 = 41;
  208.         str = bundle.getString("com.mwr.example.sieve.PIN");
  209.         if (AuthService.this.setPin(str)) {
  210.           b1 = 0;
  211.         } else {
  212.           b1 = 1;
  213.         }
  214.       } else {
  215.         sendUnrecognisedMessage();
  216.         return;
  217.       }
  218.       sendResponseMessage(4, b2, b1, null);
  219.     }
  220.   }
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement