1 /* 2 * Copyright (C) 2017 The Android Open Source Project 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.googlecode.android_scripting; 18 19 import android.app.AlertDialog; 20 import android.app.Notification; 21 import android.app.NotificationChannel; 22 import android.app.NotificationManager; 23 import android.app.PendingIntent; 24 import android.content.Context; 25 import android.content.DialogInterface; 26 import android.widget.Toast; 27 28 public class Log { 29 private static final String CHANNEL_ID = "log_channel"; Log()30 private Log() { 31 // Utility class. 32 } 33 getTag()34 private static String getTag() { 35 StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace(); 36 String fullClassName = stackTraceElements[4].getClassName(); 37 String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1); 38 int lineNumber = stackTraceElements[4].getLineNumber(); 39 return "sl4a." + className + ":" + lineNumber; 40 } 41 toast(Context context, String message)42 private static void toast(Context context, String message) { 43 Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); 44 } 45 getStringId(String identifier, Context context)46 private static int getStringId(String identifier, Context context) { 47 String packageName = context.getPackageName(); 48 return context.getResources().getIdentifier(identifier, "string", packageName); 49 } 50 createNotificationChannel(Context context, NotificationManager notificationManager)51 private static void createNotificationChannel(Context context, NotificationManager notificationManager) { 52 CharSequence name = context.getString(getStringId("notification_channel_name", context)); 53 String description = context.getString(getStringId("notification_channel_description", context)); 54 int importance = NotificationManager.IMPORTANCE_DEFAULT; 55 NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance); 56 channel.setDescription(description); 57 channel.enableLights(false); 58 channel.enableVibration(false); 59 notificationManager.createNotificationChannel(channel); 60 } 61 notify(Context context, String title, String contentTitle, String message)62 public static void notify(Context context, String title, String contentTitle, String message) { 63 android.util.Log.v(getTag(), String.format("%s %s", contentTitle, message)); 64 65 NotificationManager notificationManager = 66 (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 67 createNotificationChannel(context, notificationManager); 68 69 String packageName = context.getPackageName(); 70 int iconId = context.getResources().getIdentifier("stat_sys_warning", "drawable", packageName); 71 Notification.Builder builder = new Notification.Builder(context, CHANNEL_ID); 72 builder.setSmallIcon(iconId > 0 ? iconId : -1) 73 .setTicker(title) 74 .setWhen(0) 75 .setContentTitle(contentTitle) 76 .setContentText(message) 77 .setContentIntent(PendingIntent.getService(context, 0, null, 0)); 78 Notification note = builder.build(); 79 note.contentView.getLayoutId(); 80 notificationManager.notify(NotificationIdFactory.create(), note); 81 } 82 showDialog(final Context context, final String title, final String message)83 public static void showDialog(final Context context, final String title, final String message) { 84 android.util.Log.v(getTag(), String.format("%s %s", title, message)); 85 86 MainThread.run(context, new Runnable() { 87 @Override 88 public void run() { 89 AlertDialog.Builder builder = new AlertDialog.Builder(context); 90 builder.setTitle(title); 91 builder.setMessage(message); 92 93 DialogInterface.OnClickListener buttonListener = new DialogInterface.OnClickListener() { 94 @Override 95 public void onClick(DialogInterface dialog, int which) { 96 dialog.dismiss(); 97 } 98 }; 99 builder.setPositiveButton("Ok", buttonListener); 100 builder.show(); 101 } 102 }); 103 } 104 v(String message)105 public static void v(String message) { 106 android.util.Log.v(getTag(), message); 107 } 108 v(String message, Throwable e)109 public static void v(String message, Throwable e) { 110 android.util.Log.v(getTag(), message, e); 111 } 112 v(Context context, String message)113 public static void v(Context context, String message) { 114 toast(context, message); 115 android.util.Log.v(getTag(), message); 116 } 117 v(Context context, String message, Throwable e)118 public static void v(Context context, String message, Throwable e) { 119 toast(context, message); 120 android.util.Log.v(getTag(), message, e); 121 } 122 e(Throwable e)123 public static void e(Throwable e) { 124 android.util.Log.e(getTag(), "Error", e); 125 } 126 e(String message)127 public static void e(String message) { 128 android.util.Log.e(getTag(), message); 129 } 130 e(String message, Throwable e)131 public static void e(String message, Throwable e) { 132 android.util.Log.e(getTag(), message, e); 133 } 134 e(Context context, String message)135 public static void e(Context context, String message) { 136 toast(context, message); 137 android.util.Log.e(getTag(), message); 138 } 139 e(Context context, String message, Throwable e)140 public static void e(Context context, String message, Throwable e) { 141 toast(context, message); 142 android.util.Log.e(getTag(), message, e); 143 } 144 w(Throwable e)145 public static void w(Throwable e) { 146 android.util.Log.w(getTag(), "Warning", e); 147 } 148 w(String message)149 public static void w(String message) { 150 android.util.Log.w(getTag(), message); 151 } 152 w(String message, Throwable e)153 public static void w(String message, Throwable e) { 154 android.util.Log.w(getTag(), message, e); 155 } 156 w(Context context, String message)157 public static void w(Context context, String message) { 158 toast(context, message); 159 android.util.Log.w(getTag(), message); 160 } 161 w(Context context, String message, Throwable e)162 public static void w(Context context, String message, Throwable e) { 163 toast(context, message); 164 android.util.Log.w(getTag(), message, e); 165 } 166 d(String message)167 public static void d(String message) { 168 android.util.Log.d(getTag(), message); 169 } 170 d(String message, Throwable e)171 public static void d(String message, Throwable e) { 172 android.util.Log.d(getTag(), message, e); 173 } 174 d(Context context, String message)175 public static void d(Context context, String message) { 176 toast(context, message); 177 android.util.Log.d(getTag(), message); 178 } 179 d(Context context, String message, Throwable e)180 public static void d(Context context, String message, Throwable e) { 181 toast(context, message); 182 android.util.Log.d(getTag(), message, e); 183 } 184 i(String message)185 public static void i(String message) { 186 android.util.Log.i(getTag(), message); 187 } 188 i(String message, Throwable e)189 public static void i(String message, Throwable e) { 190 android.util.Log.i(getTag(), message, e); 191 } 192 i(Context context, String message)193 public static void i(Context context, String message) { 194 toast(context, message); 195 android.util.Log.i(getTag(), message); 196 } 197 i(Context context, String message, Throwable e)198 public static void i(Context context, String message, Throwable e) { 199 toast(context, message); 200 android.util.Log.i(getTag(), message, e); 201 } 202 } 203