-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProfileEditorActivity.java
438 lines (368 loc) · 18.8 KB
/
ProfileEditorActivity.java
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
package com.ikai.unitshop;
import android.*;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.UserProfileChangeRequest;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import de.hdodenhof.circleimageview.CircleImageView;
import static com.ikai.unitshop.Constants.READ_EXTERNAL_STORAGE_REQUEST;
public class ProfileEditorActivity extends AppCompatActivity implements View.OnClickListener {
private CircleImageView profilePhoto;
private TextView sellerNameView, mobileNumberView, shopNameView, shopAddressView,
pinCodeView;
private DatabaseReference mDatabaseReference;
private Seller sellerDetails;
private Uri imageUri;
private FirebaseUser user;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_editor);
// Set back arrow on action bar.
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
// Get current user first.
user = FirebaseAuth.getInstance().getCurrentUser();
// Get a reference to profile photo.
profilePhoto = findViewById(R.id.profile_photo);
if (user != null) {
// Get the photo url. And set it to profilePhoto.
Uri photoUrl = user.getPhotoUrl();
if (photoUrl != null) {
Glide.with(this)
.load(photoUrl)
.thumbnail(0.5f)
.into(profilePhoto);
}
}
// Get reference to firebase database.
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
// Get reference to progress bar.
progressBar = findViewById(R.id.progress_bar);
// Get reference to every text fields i.e. personal info and shop info.
sellerNameView = findViewById(R.id.seller_name);
mobileNumberView = findViewById(R.id.mobile_number);
shopNameView = findViewById(R.id.shop_name);
shopAddressView = findViewById(R.id.shop_address);
pinCodeView = findViewById(R.id.pin_code);
// Get reference to shop info edit and personal info edit icons and set on click
// listeners on those.
ImageView shopInfoEdit = findViewById(R.id.edit_shop_info);
ImageView personalInfoEdit = findViewById(R.id.edit_personal_info);
shopInfoEdit.setOnClickListener(this);
personalInfoEdit.setOnClickListener(this);
// Get a reference to camera icon click and set on click listener.
ImageView cameraIcon = findViewById(R.id.camera_icon);
cameraIcon.setOnClickListener(this);
}
@Override
protected void onResume() {
super.onResume();
if (user != null) {
// Get seller name and set it to sellerNameView.
sellerNameView.setText(user.getDisplayName());
}
// Get other fields from database and set it.
DatabaseReference temp = mDatabaseReference.child("UNiTSellers").child(user.getUid());
temp.keepSynced(true);
temp.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
sellerDetails = dataSnapshot.getValue(Seller.class);
if (sellerDetails != null) {
// Set every fields.
mobileNumberView.setText(sellerDetails.sellerMobNumber);
shopNameView.setText(sellerDetails.shopName);
shopAddressView.setText(sellerDetails.shopAddress);
pinCodeView.setText(sellerDetails.shopPinCode);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Show error why request is canceled.
showDatabaseFetchError(databaseError);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Finish the activity when user presses back arrow.
case android.R.id.home:
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
// Helper function to show error occurred during database fetching.
private void showDatabaseFetchError(DatabaseError databaseError) {
switch (databaseError.getCode()) {
case DatabaseError.DISCONNECTED:
showErrorMessageDialogue("Error in fetching the results. " +
"You are disconnected. Please try again in a little bit");
break;
case DatabaseError.NETWORK_ERROR:
showErrorMessageDialogue("Error in fetching the results. " +
"Please make sure you have internet connection.");
break;
case DatabaseError.UNAVAILABLE:
showErrorMessageDialogue("Error in fetching the results. " +
"Server is unavailable. We are sorry for that. Try " +
"again in a little bit");
break;
default:
showErrorMessageDialogue("Error in fetching the results. " +
"Some unknown error has just happened. Please try " +
"again in a little bit");
}
}
// Helper function to show the error message.
private void showErrorMessageDialogue(final String errorMessage) {
ErrorMessageDialogue msgDialog = new ErrorMessageDialogue();
Bundle bundle = new Bundle();
bundle.putString("Message", errorMessage);
msgDialog.setArguments(bundle);
msgDialog.show(getSupportFragmentManager(), "Message");
}
@Override
public void onClick(View view) {
int id = view.getId();
// Check for camera click
if (id == R.id.camera_icon) {
// Open bottom sheet provided by CropImage.
CropImage.startPickImageActivity(this);
}
// Check for personal edit icon click.
if (id == R.id.edit_personal_info) {
// Start personal info edit activity to edit personal info.
Intent intent = new Intent(this, PersonalInfoEditActivity.class);
startActivity(intent);
}
// Check for shop edit icon click.
if (id == R.id.edit_shop_info) {
// Start shop info edit activity to edit shop info.
Intent intent = new Intent(this, ShopInfoEditActivity.class);
startActivity(intent);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Check for image pick chooser.
if (requestCode == CropImage.PICK_IMAGE_CHOOSER_REQUEST_CODE) {
// Check if result is ok or not.
if (resultCode == Activity.RESULT_OK) {
imageUri = CropImage.getPickImageResultUri(this, data);
// Check read permission and crop the image.
checkPermissionAndCropShopImage();
}
}
// Check for crop request code.
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
// Check whether result is ok or not.
if (resultCode == Activity.RESULT_OK) {
// Get the result from the called activity.
CropImage.ActivityResult result = CropImage.getActivityResult(data);
// Save the image in storage and make photo url of firebase authentication
// to that storage location load the image in profilePhoto using glide.
saveImageToFirebaseAndUpdateProfilePic(result.getUri());
}
}
}
// Helper function to save image into firebase storage and point the photo url
// of firebase authentication to saved location. And set it to profile photo holder.
private void saveImageToFirebaseAndUpdateProfilePic(final Uri photoUri) {
// Show progress bar while uploading the image.
progressBar.setVisibility(View.VISIBLE);
// Store result to firebase storage.
StorageReference mStorageReference = FirebaseStorage.getInstance().getReference()
.child("UNiTShop/ProfileImages").child(FirebaseAuth.getInstance().getUid());
mStorageReference.putFile(photoUri).addOnSuccessListener(
new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
final Uri profilePicDownloadUri = taskSnapshot.getDownloadUrl();
if (profilePicDownloadUri != null) {
// Put this download url into photo url of current user.
UserProfileChangeRequest profileChangeRequest = new
UserProfileChangeRequest.Builder()
.setPhotoUri(profilePicDownloadUri)
.build();
user.updateProfile(profileChangeRequest)
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
showErrorMessageDialogue("Updating profile" +
"picture failed. Some error just " +
"happened. Please try again in a " +
"little bit");
}
}).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
// Image updating is successful so change it in
// profilePhoto also. While loading show progressbar
// and dismiss when loading is over.
Glide.with(ProfileEditorActivity.this)
.load(photoUri)
.listener(
new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed
(@Nullable GlideException e,
Object model,
Target<Drawable> target,
boolean isFirstResource) {
// Make progress bar invisible.
if (progressBar.getVisibility() ==
View.VISIBLE) {
progressBar
.setVisibility(View.GONE);
}
showErrorMessageDialogue("Error while" +
"loading the image." +
" Make sure you have good" +
"internet connection");
return false;
}
@Override
public boolean onResourceReady(
Drawable resource, Object model,
Target<Drawable> target,
DataSource dataSource,
boolean isFirstResource) {
// Make progressbar invisible.
if (progressBar.getVisibility() ==
View.VISIBLE) {
progressBar
.setVisibility(View.GONE);
}
return false;
}
}
)
.thumbnail(0.5f)
.into(profilePhoto);
}
});
}
}
}
);
}
// Helper function to check whether we have permission for reading and if not then
// take permission and then call cropShopImage() function accordingly.
private void checkPermissionAndCropShopImage() {
// For API >= 23 we need to check specifically that we have permissions to
// read external storage.
if (CropImage.isReadExternalStoragePermissionsRequired(this, imageUri)) {
// request permissions and handle the result in onRequestPermissionsResult()
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},
READ_EXTERNAL_STORAGE_REQUEST
);
} else {
// no permissions required or already granted, can start crop image activity
cropShopImage();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Check for READ_EXTERNAL_STORAGE_REQUEST request code.
if (requestCode == READ_EXTERNAL_STORAGE_REQUEST) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Call permission is granted, so call function to crop the image.
cropShopImage();
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
//Show a dialog showing the message for need of this permission.
showNeedDialogAndAskPermission();
}
}
}
}
// Helper function to show a dialog showing the need of access of external permission.
private void showNeedDialogAndAskPermission() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Allow UNiT to read external storage?");
builder.setMessage("This lets UNiT to set your profile picture and save it to our server." +
" So that we can show this image to your consumer.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(ProfileEditorActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
READ_EXTERNAL_STORAGE_REQUEST
);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
// Helper function to crop a given bitmap and save it to firebase database.
private void cropShopImage() {
// Check whether we have some imageUri.
if (imageUri != null) {
CropImage.activity(imageUri)
.setGuidelines(CropImageView.Guidelines.ON)
.setMultiTouchEnabled(true)
.setFixAspectRatio(true)
.start(this);
} else {
showErrorMessageDialogue("Some error just happened in getting the image. " +
"Please try again in a little bit.");
}
}
}