From 813e19632f55733d8ff658f8d7abc453f76d176f Mon Sep 17 00:00:00 2001 From: Sagi Dayan Date: Sat, 9 Jul 2016 03:12:49 +0300 Subject: [PATCH] Settings and photo upload --- .idea/libraries/android_android_23.xml | 9 - app/src/main/AndroidManifest.xml | 4 +- .../sagi/thesocialnotework/MainActivity.java | 8 + .../thesocialnotework/SettingsFragment.java | 244 +++++++++++------- .../ara/sagi/thesocialnotework/User.java | 59 +++++ .../ara/sagi/thesocialnotework/Utils.java | 4 +- app/src/main/res/layout/fragment_settings.xml | 140 +++++++++- 7 files changed, 358 insertions(+), 110 deletions(-) delete mode 100644 .idea/libraries/android_android_23.xml diff --git a/.idea/libraries/android_android_23.xml b/.idea/libraries/android_android_23.xml deleted file mode 100644 index a279d4a..0000000 --- a/.idea/libraries/android_android_23.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 6927407..493af43 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,7 +6,9 @@ - + + + diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/MainActivity.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/MainActivity.java index 407ca3a..142960a 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/MainActivity.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/MainActivity.java @@ -24,6 +24,7 @@ import android.support.v7.widget.Toolbar; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; +import android.widget.Toast; import com.android.volley.Response; import com.android.volley.VolleyError; @@ -205,4 +206,11 @@ public class MainActivity extends AppCompatActivity public String getUserId(){return user.getId();} + public void updateNavAvatar(){ + //Change The Avatar + Utils.URLtoImageView(menu_avatar, user.getAvatar()); + Toast.makeText(this, "Profile Updated!", Toast.LENGTH_LONG).show(); + + } + } diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SettingsFragment.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SettingsFragment.java index 88c9c79..98c5cff 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SettingsFragment.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/SettingsFragment.java @@ -1,52 +1,53 @@ package com.android_app.matan.ara.sagi.thesocialnotework; +import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; +import android.support.v4.app.ActivityCompat; import android.support.v4.app.Fragment; +import android.text.Editable; +import android.text.TextWatcher; +import android.util.Base64; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; -import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageButton; import android.widget.ImageView; +import android.widget.TextView; +import android.widget.Toast; -public class SettingsFragment extends Fragment { - private static final String ARG_PARAM1 = "param1"; - private static final String ARG_PARAM2 = "param2"; - private static final String TAG = "Settings" ; - private static final int REQUEST_IMAGE_CAPTURE = 1; - private Button cameraBtn; +import com.android.volley.Response; + +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.ByteArrayOutputStream; +import java.io.File; + +public class SettingsFragment extends Fragment implements View.OnClickListener, TextWatcher { + private static final String TAG = "[TSN/Settings]" ; + private MainActivity parent; + private ImageButton cameraBtn; private ImageView avatarImage; -// private OnFragmentInteractionListener mListener; + private EditText txt_username, txt_password, txt_email; + private Uri currentImgUri; + private TextView lbl_num_of_notes, lbl_num_of_liked; + private User user; public SettingsFragment() { // Required empty public constructor - } - /** - * Use this factory method to create a new instance of - * this fragment using the provided parameters. - * - * @param param1 Parameter 1. - * @param param2 Parameter 2. - * @return A new instance of fragment SettingsFragment. - */ - // TODO: Rename and change types and number of parameters - public static SettingsFragment newInstance(String param1, String param2) { - SettingsFragment fragment = new SettingsFragment(); - Bundle args = new Bundle(); - args.putString(ARG_PARAM1, param1); - args.putString(ARG_PARAM2, param2); - fragment.setArguments(args); - return fragment; - } + @Override public void onCreate(Bundle savedInstanceState) { @@ -58,19 +59,36 @@ public class SettingsFragment extends Fragment { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_settings, container, false); + this.parent = (MainActivity)getActivity(); + Utils.showLoadingDialog(parent, "Just a sec...", ""); + this.user = parent.getUser(); + this.cameraBtn = (ImageButton) view.findViewById(R.id.btn_camera); + this.cameraBtn.setOnClickListener(this); + this.avatarImage = (ImageView) view.findViewById(R.id.settings_userAvater_iamgeView); + this.txt_email = (EditText)view.findViewById(R.id.txt_email); + this.txt_email.addTextChangedListener(this); + this.txt_password = (EditText)view.findViewById(R.id.txt_password); + this.txt_password.addTextChangedListener(this); + this.txt_username = (EditText)view.findViewById(R.id.txt_username); + this.lbl_num_of_notes = (TextView)view.findViewById(R.id.lbl_num_of_notes); + this.lbl_num_of_liked = (TextView)view.findViewById(R.id.lbl_num_of_liked); - cameraBtn = (Button) view.findViewById(R.id.camera_button); - cameraBtn.setOnClickListener(cameraBtnListener); - avatarImage = (ImageView) view.findViewById(R.id.settings_userAvater_iamgeView); + this.txt_username.setEnabled(false); + + //Populate The data + Utils.URLtoImageView(avatarImage, user.getAvatar()); + this.txt_username.setText(user.getUsername()); + this.txt_password.setText(user.getPassword()); + this.txt_email.setText(user.getEmail()); + +// this.lbl_num_of_notes.setText(user.getNumber_of_notes()); //TODO +// this.lbl_num_of_notes.setText(user.getLiked_notes().size()); + + + Utils.dismissLoadingDialog(); return view; } -// // TODO: Rename method, update argument and hook method into UI event -// public void onButtonPressed(Uri uri) { -// if (mListener != null) { -// mListener.onFragmentInteraction(uri); -// } -// } @Override public void onAttach(Context context) { @@ -79,60 +97,110 @@ public class SettingsFragment extends Fragment { @Override public void onDetach() { - super.onDetach(); - } - -// /** -// * This interface must be implemented by activities that contain this -// * fragment to allow an interaction in this fragment to be communicated -// * to the activity and potentially other fragments contained in that -// * activity. -// *

-// * See the Android Training lesson Communicating with Other Fragments for more information. -// */ -// public interface OnFragmentInteractionListener { -// // TODO: Update argument type and name -// void onFragmentInteraction(Uri uri); -// } - - - - - private View.OnClickListener cameraBtnListener = new View.OnClickListener() - { - - public void onClick(View v) - { - - Log.d(TAG, "in camera Button"); - Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); - startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); - - } - - }; - - - @Override - public void onActivityResult(int requestCode, int resultCode, Intent data) { - Log.d(TAG, "on activity result"); - Log.d(TAG, "requestCode: "+requestCode); - Log.d(TAG, "resultCode: "+resultCode); - Log.d(TAG, "Activity.RESULT_OK: "+Activity.RESULT_OK); - if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { - Log.d(TAG, "inside if"); - - Bundle extras = data.getExtras(); - Bitmap imageBitmap= (Bitmap) extras.get("data"); - Log.d(TAG, "bitmap: "+imageBitmap.toString()); - -// Bitmap imageBitmap = (Bitmap) extras.get("data"); - avatarImage.setImageBitmap(imageBitmap); - - } + super.onDetach(); } + @Override + public void onClick(View view) { + switch(view.getId()){ + case R.id.btn_camera: + //check for permission + ActivityCompat.requestPermissions(parent, new String[]{Manifest.permission.CAMERA}, 1); + openCamera(view); + break; + } + } + + /** + * creates a new camera intent + * @param v + */ + protected void openCamera(View v) { + Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); + createDir(); + Long currentImageTimeStamp = System.currentTimeMillis(); + String timeStamp = Long.toString(currentImageTimeStamp); + File photo = new File(Utils.PHOTOS_DIR_PATH, timeStamp + ".jpg"); + currentImgUri = Uri.fromFile(photo); + intent.putExtra(MediaStore.EXTRA_OUTPUT, currentImgUri); + Log.d(TAG, "openCamera: Image URI is: " + currentImgUri); + startActivityForResult(intent, 1); + } + + protected void createDir() { + File f = new File(Utils.PHOTOS_DIR_PATH); + f.mkdirs(); + } + + @Override + public void onActivityResult(int requestCode, int resaultCode, Intent intent) { + super.onActivityResult(requestCode, requestCode, intent); + if (resaultCode == Activity.RESULT_OK) { + if (currentImgUri != null) { + saveImage(); + Log.d(TAG, "onActivityResult: Image Capured!! - Now Upload That Shit!!"); + } else { //capturing failed + Toast.makeText(getActivity(), "Failed to Get Photo, Try Again", Toast.LENGTH_LONG).show(); + Log.e(TAG, "onActivityResult: Image URI returned as NULL - Orientation Fail"); + } + } else { + Log.i(TAG, "onActivityResult: User Canceled Image taking"); + + } + } + + private void saveImage() { + Utils.showLoadingDialog(parent, "Saving Image...", "This Can Take a while"); + File myFile = new File(currentImgUri.getPath()); + + JSONObject payload = new JSONObject(); + try { + payload.put("image", ImageToBase64(myFile.getAbsolutePath())); + } catch (JSONException e) { + e.printStackTrace(); + Utils.dismissLoadingDialog(); + Toast.makeText(parent, "Failed to upload image.. Try Again", Toast.LENGTH_LONG).show(); + } + VolleyUtilSingleton.getInstance(parent).post(Utils.BASE_URL + Utils.UPLOAD_IMAGE_PATH, payload, new Response.Listener() { + @Override + public void onResponse(JSONObject response) { + Log.d(TAG, "onResponse: "+response.toString()); + try { + user.setAvatar(response.getString("image_url")); + //Populate The data + Utils.URLtoImageView(avatarImage, user.getAvatar()); + user.updateUser(parent); + } catch (JSONException e) { + e.printStackTrace(); + } + Utils.dismissLoadingDialog(); + } + }, Utils.genericErrorListener); + } + + private String ImageToBase64(String filePath){ + Bitmap bm = BitmapFactory.decodeFile(filePath); + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + bm.compress(Bitmap.CompressFormat.JPEG, 50, baos); //bm is the bitmap object + byte[] b = baos.toByteArray(); + Log.d(TAG, "ImageToBase64: "+b.length/1000); + return Base64.encodeToString(b, Base64.DEFAULT); + } + + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { + + } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + user.setEmail(txt_email.getText().toString()); + user.setPassword(txt_password.getText().toString()); + } + + @Override + public void afterTextChanged(Editable editable) { + + } } diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/User.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/User.java index d7ebd89..199a7f5 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/User.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/User.java @@ -1,6 +1,15 @@ package com.android_app.matan.ara.sagi.thesocialnotework; +import android.content.Context; +import android.content.SharedPreferences; import android.util.Log; +import android.widget.Toast; + +import com.android.volley.Response; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; import java.util.Vector; @@ -21,9 +30,13 @@ public class User { public static final String TAG = "[TSN/User]", ATTARS_DELIMETER="||" , LIKED_NOTES_DELIMETER="|"; protected String id, password, email, avatar, username; protected Vector liked_notes; + protected int number_of_notes; + protected User self; public User(String serializedUserData){ + self = this; liked_notes = new Vector<>(); + number_of_notes = 0; String[] array = serializedUserData.split("\\|\\|"); for (int i = 0 ; i < array.length ; i ++){ switch (i){ @@ -52,6 +65,14 @@ public class User { } } + public int getNumber_of_notes() { + return number_of_notes; + } + + public void setNumber_of_notes(int number_of_notes) { + this.number_of_notes = number_of_notes; + } + public String getId() { return id; } @@ -129,6 +150,44 @@ public class User { return "Id: "+id+" UserName: " + username +" Password: " +password +" email: " + email+ " Avatar: " +avatar+" Liked Notes: "+liked_notes.toString(); } + public void updateUser(final MainActivity activity){ + VolleyUtilSingleton.getInstance(activity).post(Utils.BASE_URL + "/user/upsert", this.toJSON(), new Response.Listener() { + @Override + public void onResponse(JSONObject response) { + try { + if(response.get("status") == "OK"){ + activity.updateNavAvatar(); + SharedPreferences sharedPref = activity.getSharedPreferences(MainActivity.LOCAL_DATA_TSN, Context.MODE_PRIVATE); + SharedPreferences.Editor editor = sharedPref.edit(); + editor.putString("UserData", self.Serialise()); + activity.updateNavAvatar(); + } + } catch (JSONException e) { + e.printStackTrace(); + } + } + }, Utils.genericErrorListener); + } + + public JSONObject toJSON() { + JSONObject json = new JSONObject(); + JSONArray liked = new JSONArray(); + for (int i = 0; i < liked_notes.size(); i++) { + liked.put(liked_notes.get(i)); + } + try { + json.put("id", this.id); + json.put("password", this.password); + json.put("username", this.username); + json.put("email", this.email); + json.put("avatar", this.avatar); + json.put("liked_notes_id", liked); + } catch (JSONException e) { + e.printStackTrace(); + } + + return json; + } } diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java index 00eedfe..ed80a20 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java @@ -5,6 +5,7 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; +import android.os.Environment; import android.util.Log; import android.widget.ImageView; @@ -31,9 +32,10 @@ import java.util.HashMap; public class Utils { public static final String TAG = "Utils"; - public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api"; + public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api", UPLOAD_IMAGE_PATH="/file/upload"; public static ProgressDialog progress; private static HashMap bitmapHash = new HashMap<>(); + public static final String PHOTOS_DIR_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/TheSocialNotework/"; diff --git a/app/src/main/res/layout/fragment_settings.xml b/app/src/main/res/layout/fragment_settings.xml index 799fe50..3c09fe4 100644 --- a/app/src/main/res/layout/fragment_settings.xml +++ b/app/src/main/res/layout/fragment_settings.xml @@ -1,20 +1,138 @@ - -