This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathMainActivity.java
More file actions
414 lines (364 loc) · 15.7 KB
/
Copy pathMainActivity.java
File metadata and controls
414 lines (364 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package me.angrybyte.contactsgenerator;
import android.Manifest;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresPermission;
import androidx.core.app.ActivityCompat;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Toast;
import me.angrybyte.contactsgenerator.api.Gender;
import me.angrybyte.contactsgenerator.api.Operations;
import me.angrybyte.contactsgenerator.parser.data.Person;
import me.angrybyte.contactsgenerator.service.GeneratorService;
import me.angrybyte.contactsgenerator.service.GeneratorServiceBinder;
import me.angrybyte.contactsgenerator.service.ServiceApi;
import me.angrybyte.numberpicker.view.ActualNumberPicker;
import rx.Subscriber;
import rx.Subscription;
public class MainActivity extends AppCompatActivity implements Toolbar.OnMenuItemClickListener, View.OnClickListener, ServiceConnection,
AlertDialog.OnClickListener {
private static final String TAG = MainActivity.class.getSimpleName();
private static final int PERMISSIONS_WRITE_REQUEST_CODE = 0;
private static final int PERMISSIONS_READ_REQUEST_CODE = 1;
private static final String PERSISTENT_NUMBER_OF_REQUESTS = "number";
private static final String PERSISTENT_GENDER = "gender";
private static final String PERSISTENT_USAGE_OF_PHOTOS = "use_photos";
private Dialog mConfirmationDialog;
private CheckBox mUseAvatars;
private AlertDialog mAboutDialog;
private RadioButton mMales;
private RadioButton mFemales;
private RadioButton mBothGenders;
private ProgressDialog mProgressDialog;
private ActualNumberPicker mPicker;
private boolean mServiceDisconnected;
private Subscription mSubscription;
@SuppressWarnings("ConstantConditions")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Creating " + TAG + "...");
setContentView(R.layout.activity_main);
// prepare the toolbar with title coloring
Toolbar toolbar = (Toolbar) findViewById(R.id.main_toolbar);
toolbar.setOnMenuItemClickListener(this);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.inflateMenu(R.menu.menu_main);
// initialize views
mPicker = (ActualNumberPicker) findViewById(R.id.main_number_picker);
mMales = (RadioButton) findViewById(R.id.main_gender_male);
mFemales = (RadioButton) findViewById(R.id.main_gender_female);
mUseAvatars = (CheckBox) findViewById(R.id.main_avatars_checkbox);
mBothGenders = (RadioButton) findViewById(R.id.main_gender_both);
findViewById(R.id.main_button_generate).setOnClickListener(this);
findViewById(R.id.main_button_increment).setOnClickListener(this);
findViewById(R.id.main_button_decrement).setOnClickListener(this);
// hack-fix for the buggy RadioGroup
mBothGenders.setChecked(true);
}
@Override
protected void onStart() {
super.onStart();
Log.d(TAG, "Starting " + TAG + "...");
Intent serviceIntent = new Intent(this, GeneratorService.class);
bindService(serviceIntent, this, 0);
restoreUiState();
}
@Gender
private String getChosenGender() {
if (mMales.isChecked()) {
return Operations.MALE;
} else if (mFemales.isChecked()) {
return Operations.FEMALE;
} else {
return Operations.BOTH;
}
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_button_generate: {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
generateContacts();
} else {
ActivityCompat.requestPermissions(this, new String[] {
Manifest.permission.WRITE_CONTACTS
}, PERMISSIONS_WRITE_REQUEST_CODE);
}
break;
}
case R.id.main_button_increment: {
mPicker.setValue(mPicker.getValue() + 1);
// not invalidating automatically to prevent a catastrophic number of invalidations in a single frame
mPicker.invalidate();
break;
}
case R.id.main_button_decrement: {
mPicker.setValue(mPicker.getValue() - 1);
// not invalidating automatically to prevent a catastrophic number of invalidations in a single frame
mPicker.invalidate();
break;
}
}
}
@RequiresPermission(Manifest.permission.WRITE_CONTACTS)
private void generateContacts() {
Intent generatorServiceIntent = new Intent(this, GeneratorService.class);
startService(generatorServiceIntent);
if (mServiceDisconnected) {
bindService(generatorServiceIntent, this, 0);
}
}
@RequiresPermission(Manifest.permission.READ_CONTACTS)
private void deleteGeneratedContacts() {
Intent deleteIntent = new Intent(this, GeneratorService.class);
deleteIntent.setAction(ServiceApi.DELETE_CONTACTS_ACTION);
startService(deleteIntent);
if (mServiceDisconnected) {
bindService(deleteIntent, this, 0);
}
}
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_rate: {
rateApp();
return true;
}
case R.id.action_info: {
closeAboutDialog();
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.ContactsTheme_AlertDialog);
builder.setCancelable(false);
builder.setTitle(R.string.developer_about);
builder.setMessage(R.string.developer_note);
builder.setPositiveButton(R.string.developer_share, this);
builder.setNegativeButton(R.string.developer_dont_care, this);
builder.setNeutralButton(R.string.developer_github, this);
mAboutDialog = builder.show();
return true;
}
case R.id.action_delete_generated: {
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.ContactsTheme_AlertDialog);
builder.setTitle(R.string.delete_confirmation_title);
builder.setMessage(R.string.delete_confirmation_message);
builder.setNegativeButton(R.string.delete_confirmation_negative_button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
builder.setPositiveButton(R.string.delete_confirmation_positive_button, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED) {
deleteGeneratedContacts();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[] {
Manifest.permission.READ_CONTACTS
}, PERMISSIONS_READ_REQUEST_CODE);
}
}
});
mConfirmationDialog = builder.show();
return true;
}
}
return false;
}
private void shareApp() {
String url = getString(R.string.developer_store) + getPackageName();
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("text/plain");
String text = getString(R.string.developer_check_out) + " " + url;
share.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(share, getString(R.string.app_name)));
}
private void rateApp() {
String url = getString(R.string.developer_store) + getPackageName();
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
private void viewAppSource() {
String url = getString(R.string.developer_source);
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(browserIntent);
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE: {
shareApp();
break;
}
case DialogInterface.BUTTON_NEGATIVE: {
dialog.dismiss();
break;
}
case DialogInterface.BUTTON_NEUTRAL: {
viewAppSource();
break;
}
}
}
private void closeAboutDialog() {
if (mAboutDialog != null && mAboutDialog.isShowing()) {
mAboutDialog.dismiss();
}
}
@Override
protected void onPause() {
super.onPause();
dismissConfirmationPrompt();
dismissProgressDialog();
}
@Override
protected void onStop() {
super.onStop();
Log.d(TAG, "Stopping " + TAG + "...");
saveUiState();
unbindService(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.d(TAG, "Destroy " + TAG + "...");
if (mSubscription != null && !mSubscription.isUnsubscribed()) {
mSubscription.unsubscribe();
}
}
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
Log.d(TAG, "Service " + name.getShortClassName() + " connected to " + TAG);
mServiceDisconnected = false;
final ServiceApi serviceApi = ((GeneratorServiceBinder) binder).getService();
Intent nextActivityIntent;
if (serviceApi.isDeleting()) {
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage(getString(R.string.delete_progress_message));
mProgressDialog.setCancelable(false);
mProgressDialog.show();
mSubscription = serviceApi.getDeletionsObservable().subscribe(new Subscriber<Person>() {
@Override
public void onCompleted() {
Log.d(TAG, "onCompleted: Completed!");
mProgressDialog.dismiss();
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: Error!", e);
}
@Override
public void onNext(Person person) {
Log.d(TAG, "onNext: " + person);
}
});
return;
} else if (serviceApi.getStats() == null) {
Log.d(TAG, "Stats object is null, means generating hasn't started yet");
nextActivityIntent = new Intent(this, ProgressActivity.class);
nextActivityIntent.putExtra(ProgressActivity.KEY_NUMBER, mPicker.getValue());
nextActivityIntent.putExtra(ProgressActivity.KEY_IMAGES, mUseAvatars.isChecked());
nextActivityIntent.putExtra(ProgressActivity.KEY_GENDER, getChosenGender());
} else if (serviceApi.isGenerating()) {
Log.d(TAG, "Stats object is not null, and it's still generating");
nextActivityIntent = new Intent(this, ProgressActivity.class);
} else {
Log.d(TAG, "Stats object is not null, and it's finished generating");
nextActivityIntent = new Intent(this, StatsActivity.class);
}
startActivity(nextActivityIntent);
finish();
}
@Override
public void onServiceDisconnected(ComponentName name) {
Log.d(TAG, "Service " + name.getShortClassName() + " connected to " + TAG);
mServiceDisconnected = true;
dismissProgressDialog();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case PERMISSIONS_WRITE_REQUEST_CODE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// noinspection MissingPermission | We just checked?!
generateContacts();
} else {
Toast.makeText(this, R.string.permission_denied_wow, Toast.LENGTH_SHORT).show();
}
break;
}
case PERMISSIONS_READ_REQUEST_CODE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// noinspection MissingPermission | We just checked?!
deleteGeneratedContacts();
} else {
Toast.makeText(this, R.string.permission_denied_cheeky, Toast.LENGTH_SHORT).show();
}
break;
}
default:
Log.d(TAG, "Random permission request code. Ignore.");
}
}
private void restoreUiState() {
SharedPreferences sharedPreferences = getSharedPreferences(getApplicationInfo().name, Context.MODE_PRIVATE);
int requests = sharedPreferences.getInt(PERSISTENT_NUMBER_OF_REQUESTS, 1);
String gender = sharedPreferences.getString(PERSISTENT_GENDER, Operations.BOTH);
boolean usePhotos = sharedPreferences.getBoolean(PERSISTENT_USAGE_OF_PHOTOS, true);
mPicker.setValue(requests);
switch (gender) {
case Operations.BOTH:
mBothGenders.toggle();
break;
case Operations.MALE:
mMales.toggle();
break;
case Operations.FEMALE:
mFemales.toggle();
break;
default:
Log.w(TAG, "How?");
}
mUseAvatars.setChecked(usePhotos);
}
private void saveUiState() {
SharedPreferences sharedPreferences = getSharedPreferences(getApplicationInfo().name, Context.MODE_PRIVATE);
SharedPreferences.Editor preferencesEditor = sharedPreferences.edit();
preferencesEditor.putInt(PERSISTENT_NUMBER_OF_REQUESTS, mPicker.getValue());
preferencesEditor.putString(PERSISTENT_GENDER, getChosenGender());
preferencesEditor.putBoolean(PERSISTENT_USAGE_OF_PHOTOS, mUseAvatars.isChecked());
preferencesEditor.apply();
}
private void dismissConfirmationPrompt() {
if (mConfirmationDialog != null && mConfirmationDialog.isShowing()) {
mConfirmationDialog.dismiss();
mConfirmationDialog = null;
}
}
private void dismissProgressDialog() {
if (mProgressDialog != null && mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
mProgressDialog = null;
}
}
}