User Bearbeiten V2
User Name und Bild kann bearbeitet werden Hat noch bugs, aber funktioniert im prinzip schon Bild wird nicht immer gespeichert Crop muss noch eingebaut werden
This commit is contained in:
parent
4fc6c7c85e
commit
62859a53f1
@ -1,11 +1,16 @@
|
||||
package at.smartshopper.smartshopper.activitys;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlarmManager;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.graphics.Bitmap;
|
||||
import android.graphics.BitmapFactory;
|
||||
import android.graphics.drawable.Drawable;
|
||||
import android.net.Uri;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
@ -14,19 +19,29 @@ import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ImageView;
|
||||
|
||||
import com.google.android.gms.tasks.Continuation;
|
||||
import com.google.android.gms.tasks.OnCompleteListener;
|
||||
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.storage.FirebaseStorage;
|
||||
import com.google.firebase.storage.StorageReference;
|
||||
import com.google.firebase.storage.UploadTask;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URL;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
|
||||
import at.smartshopper.smartshopper.R;
|
||||
import at.smartshopper.smartshopper.storage.ImgSaver;
|
||||
import at.smartshopper.smartshopper.db.Database;
|
||||
import at.smartshopper.smartshopper.shoppinglist.Member;
|
||||
|
||||
public class EditUser extends Activity {
|
||||
|
||||
@ -34,6 +49,8 @@ public class EditUser extends Activity {
|
||||
private ImageView userbild;
|
||||
private Button finish, chooseImg;
|
||||
private Bitmap userBitmap;
|
||||
private Database db;
|
||||
private final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
@ -42,20 +59,27 @@ public class EditUser extends Activity {
|
||||
userbild = (ImageView) findViewById(R.id.userImage);
|
||||
editname = (EditText) findViewById(R.id.editName);
|
||||
finish = (Button) findViewById(R.id.editFinish);
|
||||
chooseImg = (Button)findViewById(R.id.chooseImg);
|
||||
chooseImg = (Button) findViewById(R.id.chooseImg);
|
||||
|
||||
final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
if (user != null) {
|
||||
// Name, email address, and profile photo Url
|
||||
String name = user.getDisplayName();
|
||||
Uri photoUrl = user.getPhotoUrl();
|
||||
db = new Database();
|
||||
|
||||
userbild.setImageDrawable(LoadImageFromWebOperations(photoUrl.toString()));
|
||||
Member member = null;
|
||||
try {
|
||||
member = db.getUser(FirebaseAuth.getInstance().getUid());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
String name = member.getName();
|
||||
String photoUrl = member.getPic();
|
||||
|
||||
|
||||
userbild.setImageDrawable(LoadImageFromWebOperations(photoUrl));
|
||||
editname.setText(name);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
chooseImg.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
@ -67,7 +91,7 @@ public class EditUser extends Activity {
|
||||
pickIntent.setType("image/*");
|
||||
|
||||
Intent chooserIntent = Intent.createChooser(getIntent, "Select Image");
|
||||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
|
||||
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{pickIntent});
|
||||
|
||||
startActivityForResult(chooserIntent, 1);
|
||||
}
|
||||
@ -76,33 +100,93 @@ public class EditUser extends Activity {
|
||||
finish.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Uri uri = ImgSaver.uploadImg(userBitmap, FirebaseAuth.getInstance().getUid());
|
||||
Log.d("SmartShopper", "https://lh3.googleusercontent.com/-ZBYkQh5lTyA/AAAAAAAAAAI/AAAAAAAAEas/zCXYWfty-FE/photo.jpg");
|
||||
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
|
||||
.setDisplayName(editname.getText().toString())
|
||||
.setPhotoUri(uri)
|
||||
.build();
|
||||
ArrayList arlist = new ArrayList();
|
||||
arlist.add(userBitmap);
|
||||
arlist.add(FirebaseAuth.getInstance().getUid());
|
||||
Object[] objArr = arlist.toArray();
|
||||
ImgSaver imgSaver = new ImgSaver();
|
||||
String uri = null;
|
||||
String name = editname.getText().toString();
|
||||
try {
|
||||
uri = imgSaver.execute(objArr).get();
|
||||
|
||||
user.updateProfile(profileUpdates)
|
||||
.addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<Void> task) {
|
||||
if (task.isSuccessful()) {
|
||||
Log.d("SmartShopper", "User profile updated.");
|
||||
finish();
|
||||
|
||||
Log.d("SmartShopper", uri + " " + name);
|
||||
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
|
||||
.setDisplayName(name)
|
||||
.setPhotoUri(Uri.parse(uri))
|
||||
.build();
|
||||
try {
|
||||
Member member = db.getUser(FirebaseAuth.getInstance().getUid());
|
||||
db.updateUser(FirebaseAuth.getInstance().getUid(), member.getMsid(), name, FirebaseAuth.getInstance().getCurrentUser().getPhotoUrl().toString(), member.getEmail());
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
user.updateProfile(profileUpdates)
|
||||
.addOnCompleteListener(new OnCompleteListener<Void>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<Void> task) {
|
||||
if (task.isSuccessful()) {
|
||||
Log.d("SmartShopper", "User profile updated.");
|
||||
doRestart(EditUser.this);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
} catch (ExecutionException e) {
|
||||
e.printStackTrace();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
public static void doRestart(Context c) {
|
||||
try {
|
||||
//check if the context is given
|
||||
if (c != null) {
|
||||
//fetch the packagemanager so we can get the default launch activity
|
||||
// (you can replace this intent with any other activity if you want
|
||||
PackageManager pm = c.getPackageManager();
|
||||
//check if we got the PackageManager
|
||||
if (pm != null) {
|
||||
//create the intent with the default start activity for your application
|
||||
Intent mStartActivity = pm.getLaunchIntentForPackage(
|
||||
c.getPackageName()
|
||||
);
|
||||
if (mStartActivity != null) {
|
||||
mStartActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
|
||||
//create a pending intent so the application is restarted after System.exit(0) was called.
|
||||
// We use an AlarmManager to call this intent in 100ms
|
||||
int mPendingIntentId = 223344;
|
||||
PendingIntent mPendingIntent = PendingIntent
|
||||
.getActivity(c, mPendingIntentId, mStartActivity,
|
||||
PendingIntent.FLAG_CANCEL_CURRENT);
|
||||
AlarmManager mgr = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
|
||||
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
|
||||
//kill the application
|
||||
System.exit(0);
|
||||
} else {
|
||||
Log.e("SmartShopper", "Was not able to restart application, mStartActivity null");
|
||||
}
|
||||
} else {
|
||||
Log.e("SmartShopper", "Was not able to restart application, PM null");
|
||||
}
|
||||
} else {
|
||||
Log.e("SmartShopper", "Was not able to restart application, Context null");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
Log.e("SmartShopper", "Was not able to restart application");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data)
|
||||
{
|
||||
public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
||||
if (requestCode == 1) {
|
||||
try {
|
||||
final Uri imageUri = data.getData();
|
||||
@ -125,4 +209,44 @@ public class EditUser extends Activity {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public class ImgSaver extends AsyncTask<Object, String, String> {
|
||||
|
||||
private FirebaseStorage storage = FirebaseStorage.getInstance();
|
||||
private String downloadUriFinal = "";
|
||||
|
||||
@Override
|
||||
protected String doInBackground(Object... objects) {
|
||||
final StorageReference storageRef = storage.getReference("/" + objects[1]);
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
Bitmap bitmap = (Bitmap) objects[0];
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
||||
byte[] data = baos.toByteArray();
|
||||
|
||||
UploadTask uploadTask = storageRef.putBytes(data);
|
||||
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
|
||||
@Override
|
||||
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
|
||||
if (!task.isSuccessful()) {
|
||||
throw task.getException();
|
||||
}
|
||||
// Continue with the task to get the download URL
|
||||
return storageRef.getDownloadUrl();
|
||||
}
|
||||
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
|
||||
@Override
|
||||
public void onComplete(@NonNull Task<Uri> task) {
|
||||
if (task.isSuccessful()) {
|
||||
Uri downloadUri = task.getResult();
|
||||
Log.d("SmartShopper", downloadUri.toString());
|
||||
downloadUriFinal = downloadUri.toString();
|
||||
} else {
|
||||
System.out.println(task.getException().getMessage());
|
||||
}
|
||||
}
|
||||
});
|
||||
return downloadUriFinal;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -28,10 +28,13 @@ import com.google.firebase.auth.GoogleAuthProvider;
|
||||
import com.google.firebase.iid.FirebaseInstanceId;
|
||||
import com.google.firebase.iid.InstanceIdResult;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.sql.SQLException;
|
||||
|
||||
import at.smartshopper.smartshopper.R;
|
||||
import at.smartshopper.smartshopper.db.Database;
|
||||
import at.smartshopper.smartshopper.shoppinglist.Member;
|
||||
|
||||
public class LoginActivity extends AppCompatActivity {
|
||||
|
||||
@ -82,16 +85,22 @@ public class LoginActivity extends AppCompatActivity {
|
||||
return;
|
||||
}
|
||||
|
||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
String uid = user.getUid();
|
||||
String name = user.getDisplayName();
|
||||
String email = user.getEmail();
|
||||
String picture = " ";
|
||||
String uid = FirebaseAuth.getInstance().getUid();
|
||||
Member user;
|
||||
String name = null;
|
||||
String picture = null;
|
||||
String email = null;
|
||||
try {
|
||||
picture = user.getPhotoUrl().toString();
|
||||
} catch (Exception e) {
|
||||
user = db.getUser(uid);
|
||||
name = user.getName();
|
||||
picture = user.getPic();
|
||||
email = user.getEmail();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Get new Instance ID token
|
||||
String token = task.getResult().getToken();
|
||||
|
||||
|
@ -17,6 +17,9 @@ import com.google.firebase.auth.FirebaseAuth;
|
||||
import com.google.firebase.auth.FirebaseUser;
|
||||
import com.squareup.picasso.Picasso;
|
||||
|
||||
import org.json.JSONException;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
|
||||
import at.smartshopper.smartshopper.R;
|
||||
@ -113,13 +116,20 @@ public class ShoppinglistAdapter extends RecyclerView.Adapter<ShoppinglistAdapte
|
||||
|
||||
holder.shoppinglistColor.setBackgroundColor(cardcolor);
|
||||
|
||||
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
|
||||
if (user != null) {
|
||||
// Name, email address, and profile photo Url
|
||||
String name = user.getDisplayName();
|
||||
Uri photoUrl = user.getPhotoUrl();
|
||||
holder.ownerName.setText(name);
|
||||
Picasso.get().load(photoUrl).resize(250, 250).transform(new RoundCornersTransformation(30, 30, true, true)).into(holder.imageView);
|
||||
|
||||
String uid = FirebaseAuth.getInstance().getUid();
|
||||
|
||||
|
||||
|
||||
try {
|
||||
Member user = db.getUser(uid);
|
||||
holder.ownerName.setText(user.getName());
|
||||
Picasso.get().load(user.getPic()).resize(250, 250).transform(new RoundCornersTransformation(30, 30, true, true)).into(holder.imageView);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} catch (JSONException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
// holder.imageView.setImageDrawable(Drawable.createFromPath("@drawable/common_google_signin_btn_icon_dark"));
|
||||
|
||||
// Check if user's email is verified
|
||||
@ -130,7 +140,7 @@ public class ShoppinglistAdapter extends RecyclerView.Adapter<ShoppinglistAdapte
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
|
@ -1,65 +0,0 @@
|
||||
package at.smartshopper.smartshopper.storage;
|
||||
|
||||
import android.graphics.Bitmap;
|
||||
import android.net.Uri;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.google.android.gms.tasks.Continuation;
|
||||
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.storage.FirebaseStorage;
|
||||
import com.google.firebase.storage.StorageReference;
|
||||
import com.google.firebase.storage.UploadTask;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
public class ImgSaver {
|
||||
|
||||
private static FirebaseStorage storage = FirebaseStorage.getInstance();
|
||||
private static Uri downloadUriFinal = null;
|
||||
|
||||
public static Uri uploadImg(Bitmap bitmap, String uid){
|
||||
final StorageReference storageRef = storage.getReference("/" + uid);
|
||||
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
|
||||
byte[] data = baos.toByteArray();
|
||||
|
||||
final UploadTask uploadTask = storageRef.putBytes(data);
|
||||
uploadTask.addOnFailureListener(new OnFailureListener() {
|
||||
@Override
|
||||
public void onFailure(@NonNull Exception exception) {
|
||||
// Handle unsuccessful uploads
|
||||
Log.d("SmartShopper", exception.getMessage());
|
||||
}
|
||||
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
|
||||
@Override
|
||||
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
|
||||
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
|
||||
// ...
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
storage.getReference().child("/" + uid).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
|
||||
@Override
|
||||
public void onSuccess(Uri uri) {
|
||||
Log.d("SmartShopper", uri.toString());
|
||||
|
||||
}
|
||||
}).addOnFailureListener(new OnFailureListener() {
|
||||
@Override
|
||||
public void onFailure(@NonNull Exception exception) {
|
||||
// Handle any errors
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
return downloadUriFinal;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user