|
16 | 16 | */ |
17 | 17 | package io.github.ratul.topactivity; |
18 | 18 |
|
| 19 | +import static io.github.ratul.topactivity.receivers.NotificationReceiver.createNotificationChannel; |
| 20 | + |
19 | 21 | import android.app.Application; |
20 | | -import io.github.ratul.topactivity.model.CrashHandler; |
| 22 | +import android.content.ClipData; |
| 23 | +import android.content.ClipboardManager; |
21 | 24 | import android.content.Context; |
22 | | -import java.io.File; |
23 | | -import android.app.Activity; |
24 | | -import io.github.ratul.topactivity.ui.MainActivity; |
25 | 25 | import android.content.Intent; |
26 | | -import io.github.ratul.topactivity.ui.CrashActivity; |
| 26 | +import android.content.SharedPreferences; |
| 27 | +import android.os.Build; |
27 | 28 | import android.widget.Toast; |
28 | | -import android.os.Environment; |
29 | 29 |
|
30 | | -public class App extends Application { |
31 | | - private static App sApp; |
| 30 | +import androidx.annotation.NonNull; |
| 31 | +import androidx.core.app.NotificationManagerCompat; |
32 | 32 |
|
33 | | - @Override |
34 | | - protected void attachBaseContext(Context base) { |
35 | | - super.attachBaseContext(base); |
36 | | - sApp = this; |
37 | | - Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(this)); |
38 | | - } |
| 33 | +import io.github.ratul.topactivity.ui.CopyToClipboardActivity; |
| 34 | + |
| 35 | +public class App extends Application { |
| 36 | + private static App instance; |
| 37 | + private SharedPreferences sharedPreferences; |
| 38 | + private NotificationManagerCompat notificationManager; |
39 | 39 |
|
40 | | - @Override |
41 | | - public void onCreate() { |
42 | | - super.onCreate(); |
43 | | - } |
| 40 | + @Override |
| 41 | + public void onCreate() { |
| 42 | + super.onCreate(); |
| 43 | + instance = this; |
| 44 | + sharedPreferences = getSharedPreferences(getPackageName(), 0); |
| 45 | + notificationManager = NotificationManagerCompat.from(this); |
| 46 | + createNotificationChannel(notificationManager); |
| 47 | + } |
44 | 48 |
|
45 | | - public static String getCrashLogDir() { |
46 | | - return getCrashLogFolder().getAbsolutePath(); |
47 | | - } |
| 49 | + public SharedPreferences getSharedPreferences() { |
| 50 | + return sharedPreferences; |
| 51 | + } |
48 | 52 |
|
49 | | - public static File getCrashLogFolder() { |
50 | | - return sApp.getExternalFilesDir(null); |
51 | | - } |
| 53 | + public NotificationManagerCompat getNotificationManager() { |
| 54 | + return notificationManager; |
| 55 | + } |
52 | 56 |
|
53 | | - public static App getApp() { |
54 | | - return sApp; |
55 | | - } |
| 57 | + public static App getInstance() { |
| 58 | + return instance; |
| 59 | + } |
56 | 60 |
|
57 | | - public static void showToast(String str, int length) { |
58 | | - Toast.makeText(getApp(), str, length).show(); |
59 | | - } |
| 61 | + public static void copyString(Context context, String str, String msg) { |
| 62 | + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { |
| 63 | + ClipboardManager clipboard = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); |
| 64 | + ClipData clip = ClipData.newPlainText(context.getString(R.string.app_name), str); |
| 65 | + clipboard.setPrimaryClip(clip); |
| 66 | + } else { |
| 67 | + Intent copyActivity = new Intent(context, CopyToClipboardActivity.class) |
| 68 | + .putExtra(Intent.EXTRA_TEXT, str) |
| 69 | + .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 70 | + context.startActivity(copyActivity); |
| 71 | + } |
| 72 | + showToast(context, msg); |
| 73 | + } |
60 | 74 |
|
| 75 | + public static void showToast(@NonNull Context context, @NonNull String message) { |
| 76 | + try { |
| 77 | + Toast.makeText(context, message, Toast.LENGTH_SHORT).show(); |
| 78 | + } catch (Throwable ignored) { |
| 79 | + try { |
| 80 | + Toast.makeText(instance, message, Toast.LENGTH_SHORT).show(); |
| 81 | + } catch (Throwable ignored2) { |
| 82 | + } |
| 83 | + } |
| 84 | + } |
61 | 85 | } |
0 commit comments