OSDN Git Service

rev version;
[android-x86/external-koush-Superuser.git] / Superuser / src / com / koushikdutta / superuser / SuReceiver.java
1 /*
2  * Copyright (C) 2013 Koushik Dutta (@koush)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.koushikdutta.superuser;
18
19 import android.app.NotificationManager;
20 import android.app.PendingIntent;
21 import android.content.BroadcastReceiver;
22 import android.content.Context;
23 import android.content.Intent;
24 import android.support.v4.app.NotificationCompat;
25 import android.widget.Toast;
26
27 import com.koushikdutta.superuser.db.LogEntry;
28 import com.koushikdutta.superuser.db.SuperuserDatabaseHelper;
29 import com.koushikdutta.superuser.db.UidPolicy;
30 import com.koushikdutta.superuser.util.Settings;
31
32 public class SuReceiver extends BroadcastReceiver {
33     @Override
34     public void onReceive(final Context context, Intent intent) {
35         if (intent == null)
36             return;
37         
38         String command = intent.getStringExtra("command");
39         if (command == null)
40             return;
41         int uid = intent.getIntExtra("uid", -1);
42         if (uid == -1)
43             return;
44         int desiredUid = intent.getIntExtra("desired_uid", -1);
45         if (desiredUid == -1)
46             return;
47         String action = intent.getStringExtra("action");
48         if (action == null)
49             return;
50         String fromName = intent.getStringExtra("from_name");
51         String desiredName = intent.getStringExtra("desired_name");
52
53         final LogEntry le = new LogEntry();
54         le.uid = uid;
55         le.command = command;
56         le.action = action;
57         le.desiredUid = desiredUid;
58         le.desiredName = desiredName;
59         le.username = fromName;
60         le.date = (int)(System.currentTimeMillis() / 1000);
61         le.getPackageInfo(context);
62
63         UidPolicy u = SuperuserDatabaseHelper.addLog(context, le);
64
65         String toast;
66         if (UidPolicy.ALLOW.equals(action)) {
67             toast = context.getString(R.string.superuser_granted, le.getName());
68         }
69         else {
70             toast = context.getString(R.string.superuser_denied, le.getName());
71         }
72
73         if (u != null && !u.notification)
74             return;
75
76         switch (Settings.getNotificationType(context)) {
77         case Settings.NOTIFICATION_TYPE_NOTIFICATION:
78             NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
79             builder.setTicker(toast)
80             .setAutoCancel(true)
81             .setContentIntent(PendingIntent.getActivity(context, 0, new Intent(), 0))
82             .setContentTitle(context.getString(R.string.superuser))
83             .setContentText(toast)
84             .setSmallIcon(R.drawable.ic_stat_notification);
85             
86             NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
87             nm.notify(NOTIFICATION_ID, builder.getNotification());
88             break;
89         case Settings.NOTIFICATION_TYPE_TOAST:
90             Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
91             break;
92         }
93     }
94     
95     private static final int NOTIFICATION_ID = 4545;
96 }