-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathReceiveTextActivity.java
More file actions
382 lines (335 loc) · 15.7 KB
/
ReceiveTextActivity.java
File metadata and controls
382 lines (335 loc) · 15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.volla.launcher.activity;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.AlarmManager ;
import android.content.pm.PackageManager;
import android.content.pm.LauncherApps;
import android.content.pm.LauncherApps.PinItemRequest;
import android.content.pm.ShortcutInfo;
import android.content.Intent;
import android.view.Window;
import android.view.View;
import android.view.WindowManager;
import android.app.UiModeManager;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.fonts.SystemFonts;
import android.graphics.fonts.Font;
import android.util.Log;
import android.os.Bundle;
import android.os.Build;
import android.os.Handler;
import android.content.Context;
import android.content.IntentFilter;
import android.content.res.Configuration;
import android.util.Base64;
import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Set;
import java.io.ByteArrayOutputStream;
import androidnative.SystemDispatcher;
import androidnative.AndroidNativeActivity;
import android.widget.Toast;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import com.volla.launcher.R;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import com.volla.launcher.util.NotificationPlugin;
import android.content.ServiceConnection;
import android.content.ComponentName;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import com.volla.smssdk.SMSUpdateManager;
import android.os.Looper;
import android.content.pm.ActivityInfo;
public class ReceiveTextActivity extends AndroidNativeActivity implements SMSUpdateManager.ServiceConnectionListener
{
private static final String TAG = "ReceiveTextActivity";
public static final String GOT_TEXT = "volla.launcher.receiveTextResponse";
public static final String CHECK_SHORTCUT = "volla.launcher.checkNewShortcut";
public static final String GOT_SHORTCUT = "volla.launcher.receivedShortcut";
public static final String UIMODE_CHANGED = "volla.launcher.uiModeChanged";
public List<String> smsPid;
private Handler handler;
private NotificationBroadcastReceiver notificationBroadcastReceiver;
public static ReceiveTextActivity instance;
private String channel_d;
SMSUpdateManager smsUpdateManager;
private static Map pendingShortcutMessage;
static {
SystemDispatcher.addListener(new SystemDispatcher.Listener() {
public void onDispatched(String type, Map dmessage) {
if (type.equals(CHECK_SHORTCUT)) {
Runnable runnable = new Runnable () {
public void run() {
if (pendingShortcutMessage != null) {
SystemDispatcher.dispatch(GOT_SHORTCUT, pendingShortcutMessage);
pendingShortcutMessage = null;
}
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
});
}
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreated() called");
// Set<Font> sfs = SystemFonts.getAvailableFonts();
// Iterator<Font> fontIterator = sfs.iterator();
// while(fontIterator.hasNext()) {
// Font f = fontIterator.next();
// Log.d(TAG, "Font: " + f.getFile());
// }
// Workaround for blank activity
// https://forum.qt.io/topic/90189/android-e-qt-java-surface-1-not-found/2
if (instance != null) {
Log.d(TAG, "App is already running... this won't work");
Intent mStartActivity = new Intent(this, ReceiveTextActivity.class);
if (getIntent().hasExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST)) {
mStartActivity.putExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST,
(LauncherApps.PinItemRequest)getIntent().getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST));
mStartActivity.setAction(LauncherApps.ACTION_CONFIRM_PIN_SHORTCUT);
}
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(
this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
} else if (getIntent().hasExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST)) {
LauncherApps.PinItemRequest pinItemRequest = getIntent().getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST);
if (pinItemRequest.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT) {
boolean success = pinItemRequest.accept();
Log.d(TAG, "Shortcut is accepted: " + success);
ShortcutInfo shortcutInfo = pinItemRequest.getShortcutInfo();
Log.d(TAG, "New shortcut: " + shortcutInfo.getId());
LauncherApps launcher = (LauncherApps) getSystemService(Context.LAUNCHER_APPS_SERVICE);
Drawable shortcutIcon = launcher.getShortcutIconDrawable(shortcutInfo,0);
Map reply = new HashMap();
reply.put("shortcutId", shortcutInfo.getId() );
reply.put("package", shortcutInfo.getPackage() );
reply.put("label", shortcutInfo.getShortLabel().toString() );
reply.put("icon", drawableToBase64(shortcutIcon) );
pendingShortcutMessage = reply;
} else {
Log.w(TAG, "Not valid pin item request");
}
}
instance = this;
Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
window.setNavigationBarColor(Color.TRANSPARENT);
window.setStatusBarColor(Color.TRANSPARENT ) ;
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setDecorFitsSystemWindows(false);
window.setStatusBarContrastEnforced(false);
window.setNavigationBarContrastEnforced(false);
} else {
window.setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
}
notificationBroadcastReceiver = new NotificationBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.volla.launcher.notification");
registerReceiver(notificationBroadcastReceiver, intentFilter);
NotificationPlugin.getInstance(ReceiveTextActivity.this).registerListener();
Log.d(TAG, "Android activity created");
handler = new Handler(Looper.getMainLooper());
if (isTablet(this)) {
Log.d(TAG, "Runnning on tablet");
// Allow both portrait and landscape on tablets
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
} else {
Log.d(TAG, "Runnning on phone");
// Force portrait mode on phones
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
private boolean isTablet(Context context) {
Log.d(TAG, "isTablet() called");
return (ReceiveTextActivity.this.getResources().getConfiguration().screenLayout
& Configuration.SCREENLAYOUT_SIZE_MASK)
>= Configuration.SCREENLAYOUT_SIZE_LARGE;
}
public void connectSmsUpdateManager(Context ctx, List<String> smsItems) {
this.smsPid = smsItems;
smsUpdateManager = new SMSUpdateManager(ctx, this);
smsUpdateManager.start();
}
@Override
public void onServiceConnected() {
if(smsPid != null && smsPid.size() > 0){
Log.d(TAG, "SMS database update called ");
int item = smsUpdateManager.smsUpdate(smsPid);
Log.d(TAG, "SMS database updated "+ item + "for sms id "+smsPid);
}
Log.d(TAG, "SMS database updated ");
}
@Override
public void onServiceDisconnected() {
}
@Override
protected void onDestroy() {
super.onDestroy();
unregisterReceiver(notificationBroadcastReceiver);
}
@Override
public void onNewIntent(Intent intent) {
Log.d(TAG, "onNewIntend() called");
super.onNewIntent(intent);
String action = intent.getAction();
String type = intent.getType();
Log.d(TAG, "Intend: " + intent + ", Action: " + action + ", type: " + type);
if (Intent.ACTION_SEND.equals(action) && type != null && "text/plain".equals(type)) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (sharedText != null) {
Map reply = new HashMap();
reply.put("sharedText", sharedText );
SystemDispatcher.dispatch(GOT_TEXT, reply);
}
Log.d(TAG, "Shared text: " + sharedText);
} else if (LauncherApps.ACTION_CONFIRM_PIN_SHORTCUT.equals(action)) {
LauncherApps.PinItemRequest pinItemRequest = intent.getParcelableExtra(LauncherApps.EXTRA_PIN_ITEM_REQUEST);
if (pinItemRequest.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT) {
Log.d(TAG, "Will accept shortcut");
boolean success = pinItemRequest.accept();
Log.d(TAG, "Shortcut is accepted: " + success);
ShortcutInfo shortcutInfo = pinItemRequest.getShortcutInfo();
Log.d(TAG, "New shortcut: " + shortcutInfo.getId());
LauncherApps launcher = (LauncherApps) getSystemService(Context.LAUNCHER_APPS_SERVICE);
Drawable shortcutIcon = launcher.getShortcutIconDrawable(shortcutInfo,0);
Map reply = new HashMap();
reply.put("shortcutId", shortcutInfo.getId() );
reply.put("package", shortcutInfo.getPackage() );
reply.put("label", shortcutInfo.getShortLabel().toString() );
reply.put("icon", drawableToBase64(shortcutIcon) );
pendingShortcutMessage = reply;
} else {
Log.w(TAG, "Not valid pin item request");
}
}
}
@Override
public void onResume() {
super.onResume();
Log.d(TAG, "On Resume called");
NotificationPlugin.getInstance(ReceiveTextActivity.this).registerListener();
// todo: Adopt ui mode
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
Log.d(TAG, "Received permission result " + grantResults[0] + " for permission " + String.join(",", permissions));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2) {
// TODO: Re-check statistic permission
Log.d(TAG, "Result for permission request: " + resultCode);
}
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.d(TAG, "Config changed");
int currentNightMode = newConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK;
Map message = new HashMap();
switch (currentNightMode) {
case Configuration.UI_MODE_NIGHT_NO:
// Night mode is not active, we're using the light theme
Log.d(TAG, "Night mode enabled");
message.put("uiMode", 0 );
SystemDispatcher.dispatch(UIMODE_CHANGED, message);
break;
case Configuration.UI_MODE_NIGHT_YES:
// Night mode is active, we're using dark theme
Log.d(TAG, "Light mode enabled");
message.put("uiMode", 1 );
SystemDispatcher.dispatch(UIMODE_CHANGED, message);
break;
}
}
@Override
public void onBackPressed() {
Log.d(TAG, "Prevent closing app");
}
private String drawableToBase64 (Drawable drawable) {
Bitmap bitmap = null;
if (drawable instanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
if(bitmapDrawable.getBitmap() != null) {
bitmap = bitmapDrawable.getBitmap();
}
} else {
if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
} else {
bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
}
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
return Base64.encodeToString(imageBytes, Base64.NO_WRAP);
}
private void createNotification(Intent intent){
Log.d("Arvindvolla", "Creating Notification");
Bitmap largeIcon = null;
byte [] bitMapByte = intent.getByteArrayExtra("largeIcon");
if(bitMapByte != null){
largeIcon = BitmapFactory.decodeByteArray(bitMapByte, 0,bitMapByte.length);
}
NotificationManager notificationManager = (NotificationManager)
this.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this,channel_d)
.setSmallIcon(R.drawable.icon)
.setAutoCancel(true)
.setContentTitle(intent.getStringExtra("title"))
.setContentText(intent.getStringExtra("body"))
.setLargeIcon(largeIcon)
.setDefaults(Notification.DEFAULT_LIGHTS| Notification.DEFAULT_SOUND)
.setTicker("Message")
.setPriority(Notification.PRIORITY_HIGH)
.build();
notificationManager.notify("VollaOS", 1, notification);
}
public class NotificationBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("Arvindvolla", "Received Notification Broadcast");
int receivedNotificationCode = intent.getIntExtra("Notification Code",-1);
channel_d = intent.getStringExtra("channel_d");
/*
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channel_d,"VollaLauncher",
NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
if(manager != null){
manager.createNotificationChannel(channel);
}
} */
createNotification(intent);
}
}
}