OSDN Git Service

LICENSE
[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 com.koushikdutta.superuser.db.LogEntry;
20 import com.koushikdutta.superuser.db.SuDatabaseHelper;
21 import com.koushikdutta.superuser.db.UidPolicy;
22 import com.koushikdutta.superuser.util.Settings;
23
24 import android.app.NotificationManager;
25 import android.content.BroadcastReceiver;
26 import android.content.Context;
27 import android.content.Intent;
28 import android.support.v4.app.NotificationCompat;
29 import android.widget.Toast;
30
31 public class SuReceiver extends BroadcastReceiver {
32     @Override
33     public void onReceive(Context context, Intent intent) {
34         if (intent == null)
35             return;
36         
37         String command = intent.getStringExtra("command");
38         if (command == null)
39             return;
40         int uid = intent.getIntExtra("uid", -1);
41         if (uid == -1)
42             return;
43         int desiredUid = intent.getIntExtra("desired_uid", -1);
44         if (desiredUid == -1)
45             return;
46         String action = intent.getStringExtra("action");
47         if (action == null)
48             return;
49         String fromName = intent.getStringExtra("from_name");
50         String desiredName = intent.getStringExtra("desired_name");
51
52         LogEntry le = new LogEntry();
53         le.uid = uid;
54         le.command = command;
55         le.action = action;
56         le.desiredUid = desiredUid;
57         le.desiredName = desiredName;
58         le.username = fromName;
59         SuDatabaseHelper.addLog(context, le);
60
61         String toast;
62         if (UidPolicy.ALLOW.equals(action)) {
63             toast = context.getString(R.string.superuser_granted, le.getName());
64         }
65         else {
66             toast = context.getString(R.string.superuser_denied, le.getName());
67         }
68
69         switch (Settings.getNotificationType(context)) {
70         case Settings.NOTIFICATION_TYPE_NOTIFICATION:
71             NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
72             builder.setTicker(toast)
73             .setContentTitle(context.getString(R.string.superuser))
74             .setContentText(toast)
75             .setSmallIcon(R.drawable.ic_stat_notification);
76             
77             NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
78             nm.notify(NOTIFICATION_ID, builder.getNotification());
79             break;
80         case Settings.NOTIFICATION_TYPE_TOAST:
81             Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
82             break;
83         }
84     }
85     
86     private static final int NOTIFICATION_ID = 4545;
87 }