Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.mwr.example.sieve;
- import android.app.NotificationManager;
- import android.app.PendingIntent;
- import android.app.Service;
- import android.content.ContentValues;
- import android.content.CursorLoader;
- import android.content.Intent;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.HandlerThread;
- import android.os.IBinder;
- import android.os.Looper;
- import android.os.Message;
- import android.os.Messenger;
- import android.os.RemoteException;
- import android.support.v4.app.NotificationCompat;
- import android.util.Log;
- public class AuthService extends Service {
- static final int MSG_CHECK = 2354;
- static final int MSG_CHECK_IF_INITALISED = 2;
- static final int MSG_FIRST_LAUNCH = 4;
- static final int MSG_SAY_HELLO = 1;
- static final int MSG_SET = 6345;
- static final int MSG_UNREGISTER = -1;
- public static final String PASSWORD = "com.mwr.example.sieve.PASSWORD";
- public static final String PIN = "com.mwr.example.sieve.PIN";
- private static final String TAG = "m_AuthService";
- static final int TYPE_KEY = 7452;
- static final int TYPE_PIN = 9234;
- private int NOTIFICATION = 2131034112;
- private NotificationManager nManager;
- private Messenger responseHandler;
- private Messenger serviceHandler;
- private Looper serviceLooper;
- private String getKey() {
- Cursor cursor = (new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "Password" }, null, null, null)).loadInBackground();
- cursor.moveToFirst();
- return cursor.getString(cursor.getColumnIndex("Password"));
- }
- private boolean setKey(String paramString) {
- ContentValues contentValues = new ContentValues();
- contentValues.put("Password", paramString);
- return (getContentResolver().insert(DBContentProvider.KEYS_URI, contentValues) != null);
- }
- private boolean setPin(String paramString) {
- ContentValues contentValues = new ContentValues();
- contentValues.put("pin", paramString);
- return (getContentResolver().update(DBContentProvider.KEYS_URI, contentValues, "pin IS NULL", null) > 0);
- }
- private void showNotification() {
- NotificationCompat.Builder builder = (new NotificationCompat.Builder(this)).setSmallIcon(2130837504).setContentTitle(getText(2131034112)).setOngoing(true).setContentText("Click to access your passwords");
- Intent intent = new Intent(this, MainLoginActivity.class);
- intent.addFlags(1048576);
- builder.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, intent, 268435456));
- this.nManager.notify(this.NOTIFICATION, builder.build());
- }
- private boolean verifyKey(String paramString) { return ((new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "Password" }, "Password = ?", new String[] { paramString }, null)).loadInBackground().getCount() == 1); }
- private boolean verifyPin(String paramString) { return ((new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "pin" }, "pin = ?", new String[] { paramString }, null)).loadInBackground().getCount() == 1); }
- public boolean checkKeyExists() { return ((new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "Password" }, null, null, null)).loadInBackground().getCount() > 0); }
- public boolean checkPinExists() {
- Cursor cursor = (new CursorLoader(this, DBContentProvider.KEYS_URI, new String[] { "pin" }, null, null, null)).loadInBackground();
- boolean bool = false;
- cursor.moveToFirst();
- for (byte b = 0;; b++) {
- if (b >= cursor.getCount())
- return bool;
- if (cursor.getString(cursor.getColumnIndex("pin")) != null) {
- bool = true;
- } else {
- bool = false;
- }
- }
- }
- public IBinder onBind(Intent paramIntent) { return this.serviceHandler.getBinder(); }
- public void onCreate() {
- this.nManager = (NotificationManager)getSystemService("notification");
- HandlerThread handlerThread = new HandlerThread("m_AuthService", 10);
- handlerThread.start();
- this.serviceLooper = handlerThread.getLooper();
- this.serviceHandler = new Messenger(new MessageHandler(this, this.serviceLooper));
- }
- public void onDestroy() { this.nManager.cancelAll(); }
- public int onStartCommand(Intent paramIntent, int paramInt1, int paramInt2) { return 1; }
- private final class MessageHandler extends Handler {
- public MessageHandler(Looper param1Looper) { super(param1Looper); }
- private void sendResponseMessage(int param1Int1, int param1Int2, int param1Int3, Bundle param1Bundle) {
- try {
- Message message = Message.obtain(null, param1Int1, param1Int2, param1Int3);
- if (param1Bundle != null)
- message.setData(param1Bundle);
- AuthService.this.responseHandler.send(message);
- return;
- } catch (RemoteException param1Bundle) {
- Log.e("m_AuthService", "Unable to send message: " + param1Int1);
- return;
- }
- }
- private void sendUnrecognisedMessage() {
- try {
- Message message = Message.obtain(null, 111111, 122222, 1, null);
- AuthService.this.responseHandler.send(message);
- return;
- } catch (RemoteException remoteException) {
- Log.e("m_AuthService", "Unable to send message");
- return;
- }
- }
- public void handleMessage(Message param1Message) {
- byte b2;
- byte b1;
- String str;
- AuthService.this.responseHandler = param1Message.replyTo;
- Bundle bundle = (Bundle)param1Message.obj;
- switch (param1Message.what) {
- default:
- Log.e("m_AuthService", "Error: unrecognized command: " + param1Message.what);
- sendUnrecognisedMessage();
- super.handleMessage(param1Message);
- return;
- case 2354:
- if (param1Message.arg1 == 7452) {
- b2 = 42;
- str = bundle.getString("com.mwr.example.sieve.PASSWORD");
- if (AuthService.this.verifyKey(str)) {
- AuthService.this.showNotification();
- b1 = 0;
- Bundle bundle1 = bundle;
- } else {
- b1 = 1;
- str = bundle;
- }
- } else if (str.arg1 == 9234) {
- b2 = 41;
- String str1 = bundle.getString("com.mwr.example.sieve.PIN");
- if (AuthService.this.verifyPin(str1)) {
- Bundle bundle1 = new Bundle();
- bundle1.putString("com.mwr.example.sieve.PASSWORD", AuthService.this.getKey());
- b1 = 0;
- } else {
- b1 = 1;
- str = bundle;
- }
- } else {
- sendUnrecognisedMessage();
- return;
- }
- sendResponseMessage(5, b2, b1, str);
- return;
- case 4:
- if (AuthService.this.checkKeyExists()) {
- if (AuthService.this.checkPinExists()) {
- b1 = 31;
- } else {
- b1 = 32;
- }
- } else {
- b1 = 33;
- }
- sendResponseMessage(3, b1, 1, null);
- return;
- case 6345:
- break;
- }
- if (str.arg1 == 7452) {
- b2 = 42;
- str = bundle.getString("com.mwr.example.sieve.PASSWORD");
- if (AuthService.this.setKey(str)) {
- b1 = 0;
- } else {
- b1 = 1;
- }
- } else if (str.arg1 == 9234) {
- b2 = 41;
- str = bundle.getString("com.mwr.example.sieve.PIN");
- if (AuthService.this.setPin(str)) {
- b1 = 0;
- } else {
- b1 = 1;
- }
- } else {
- sendUnrecognisedMessage();
- return;
- }
- sendResponseMessage(4, b2, b1, null);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement