OSDN Git Service

Merge pull request #4 from Decad3nce/aesthetics
[android-x86/external-koush-Superuser.git] / Superuser / src / com / koushikdutta / superuser / SuReceiver.java
1 package com.koushikdutta.superuser;
2
3 import com.koushikdutta.superuser.db.LogEntry;
4 import com.koushikdutta.superuser.db.SuDatabaseHelper;
5 import com.koushikdutta.superuser.db.UidPolicy;
6 import com.koushikdutta.superuser.util.Settings;
7
8 import android.app.NotificationManager;
9 import android.content.BroadcastReceiver;
10 import android.content.Context;
11 import android.content.Intent;
12 import android.support.v4.app.NotificationCompat;
13 import android.widget.Toast;
14
15 public class SuReceiver extends BroadcastReceiver {
16     @Override
17     public void onReceive(Context context, Intent intent) {
18         if (intent == null)
19             return;
20         
21         String command = intent.getStringExtra("command");
22         if (command == null)
23             return;
24         int uid = intent.getIntExtra("uid", -1);
25         if (uid == -1)
26             return;
27         int desiredUid = intent.getIntExtra("desired_uid", -1);
28         if (desiredUid == -1)
29             return;
30         String action = intent.getStringExtra("action");
31         if (action == null)
32             return;
33         String fromName = intent.getStringExtra("from_name");
34         String desiredName = intent.getStringExtra("desired_name");
35
36         LogEntry le = new LogEntry();
37         le.uid = uid;
38         le.command = command;
39         le.action = action;
40         le.desiredUid = desiredUid;
41         le.desiredName = desiredName;
42         le.username = fromName;
43         SuDatabaseHelper.addLog(context, le);
44
45         String toast;
46         if (UidPolicy.ALLOW.equals(action)) {
47             toast = context.getString(R.string.superuser_granted, le.getName());
48         }
49         else {
50             toast = context.getString(R.string.superuser_denied, le.getName());
51         }
52
53         switch (Settings.getNotificationType(context)) {
54         case Settings.NOTIFICATION_TYPE_NOTIFICATION:
55             NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
56             builder.setTicker(toast)
57             .setContentTitle(context.getString(R.string.app_name))
58             .setContentText(toast)
59             .setSmallIcon(R.drawable.ic_stat_notification);
60             
61             NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
62             nm.notify(NOTIFICATION_ID, builder.getNotification());
63             break;
64         case Settings.NOTIFICATION_TYPE_TOAST:
65             Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
66             break;
67         }
68     }
69     
70     private static final int NOTIFICATION_ID = 4545;
71 }