2 * Copyright (C) 2013 Koushik Dutta (@koush)
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.koushikdutta.superuser;
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;
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;
32 public class SuReceiver extends BroadcastReceiver {
34 public void onReceive(final Context context, Intent intent) {
38 String command = intent.getStringExtra("command");
41 int uid = intent.getIntExtra("uid", -1);
44 int desiredUid = intent.getIntExtra("desired_uid", -1);
47 String action = intent.getStringExtra("action");
50 String fromName = intent.getStringExtra("from_name");
51 String desiredName = intent.getStringExtra("desired_name");
53 final LogEntry le = new LogEntry();
57 le.desiredUid = desiredUid;
58 le.desiredName = desiredName;
59 le.username = fromName;
60 le.date = (int)(System.currentTimeMillis() / 1000);
61 le.getPackageInfo(context);
63 UidPolicy u = SuperuserDatabaseHelper.addLog(context, le);
66 if (UidPolicy.ALLOW.equals(action)) {
67 toast = context.getString(R.string.superuser_granted, le.getName());
70 toast = context.getString(R.string.superuser_denied, le.getName());
73 if (u != null && !u.notification)
76 switch (Settings.getNotificationType(context)) {
77 case Settings.NOTIFICATION_TYPE_NOTIFICATION:
78 NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
79 builder.setTicker(toast)
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);
86 NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
87 nm.notify(NOTIFICATION_ID, builder.getNotification());
89 case Settings.NOTIFICATION_TYPE_TOAST:
90 Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
95 private static final int NOTIFICATION_ID = 4545;