Settings and photo upload

This commit is contained in:
Sagi Dayan 2016-07-09 03:12:49 +03:00
parent e72bbb3fcf
commit 813e19632f
7 changed files with 358 additions and 110 deletions

View file

@ -1,9 +0,0 @@
<component name="libraryTable">
<library name="android-android-23">
<CLASSES>
<root url="jar://$USER_HOME$/AppData/Local/Android/sdk/platforms/android-23/android.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>

View file

@ -6,7 +6,9 @@
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission." /> <uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- To auto-complete the email text field in the login form with the user's emails --> <!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" />

View file

@ -24,6 +24,7 @@ import android.support.v7.widget.Toolbar;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast;
import com.android.volley.Response; import com.android.volley.Response;
import com.android.volley.VolleyError; import com.android.volley.VolleyError;
@ -205,4 +206,11 @@ public class MainActivity extends AppCompatActivity
public String getUserId(){return user.getId();} 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();
}
} }

View file

@ -1,52 +1,53 @@
package com.android_app.matan.ara.sagi.thesocialnotework; package com.android_app.matan.ara.sagi.thesocialnotework;
import android.Manifest;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri; import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Base64;
import android.util.Log; import android.util.Log;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class SettingsFragment extends Fragment { import com.android.volley.Response;
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2"; import org.json.JSONException;
private static final String TAG = "Settings" ; import org.json.JSONObject;
private static final int REQUEST_IMAGE_CAPTURE = 1;
private Button cameraBtn; 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 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() { public SettingsFragment() {
// Required empty public constructor // 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 @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
@ -58,19 +59,36 @@ public class SettingsFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_settings, container, false); 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); this.txt_username.setEnabled(false);
cameraBtn.setOnClickListener(cameraBtnListener);
avatarImage = (ImageView) view.findViewById(R.id.settings_userAvater_iamgeView); //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; 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 @Override
public void onAttach(Context context) { public void onAttach(Context context) {
@ -82,57 +100,107 @@ public class SettingsFragment extends Fragment {
super.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.
// * <p>
// * See the Android Training lesson <a href=
// * "http://developer.android.com/training/basics/fragments/communicating.html"
// * >Communicating with Other Fragments</a> 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 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) { public void onClick(View view) {
Log.d(TAG, "on activity result"); switch(view.getId()){
Log.d(TAG, "requestCode: "+requestCode); case R.id.btn_camera:
Log.d(TAG, "resultCode: "+resultCode); //check for permission
Log.d(TAG, "Activity.RESULT_OK: "+Activity.RESULT_OK); ActivityCompat.requestPermissions(parent, new String[]{Manifest.permission.CAMERA}, 1);
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) { openCamera(view);
Log.d(TAG, "inside if"); break;
}
}
Bundle extras = data.getExtras(); /**
Bitmap imageBitmap= (Bitmap) extras.get("data"); * creates a new camera intent
Log.d(TAG, "bitmap: "+imageBitmap.toString()); * @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);
}
// Bitmap imageBitmap = (Bitmap) extras.get("data"); protected void createDir() {
avatarImage.setImageBitmap(imageBitmap); 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<JSONObject>() {
@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) {
}
} }

View file

@ -1,6 +1,15 @@
package com.android_app.matan.ara.sagi.thesocialnotework; package com.android_app.matan.ara.sagi.thesocialnotework;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log; 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; import java.util.Vector;
@ -21,9 +30,13 @@ public class User {
public static final String TAG = "[TSN/User]", ATTARS_DELIMETER="||" , LIKED_NOTES_DELIMETER="|"; public static final String TAG = "[TSN/User]", ATTARS_DELIMETER="||" , LIKED_NOTES_DELIMETER="|";
protected String id, password, email, avatar, username; protected String id, password, email, avatar, username;
protected Vector<String> liked_notes; protected Vector<String> liked_notes;
protected int number_of_notes;
protected User self;
public User(String serializedUserData){ public User(String serializedUserData){
self = this;
liked_notes = new Vector<>(); liked_notes = new Vector<>();
number_of_notes = 0;
String[] array = serializedUserData.split("\\|\\|"); String[] array = serializedUserData.split("\\|\\|");
for (int i = 0 ; i < array.length ; i ++){ for (int i = 0 ; i < array.length ; i ++){
switch (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() { public String getId() {
return id; 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(); 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<JSONObject>() {
@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;
}
} }

View file

@ -5,6 +5,7 @@ import android.content.Context;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.BitmapFactory; import android.graphics.BitmapFactory;
import android.os.AsyncTask; import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log; import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
@ -31,9 +32,10 @@ import java.util.HashMap;
public class Utils { public class Utils {
public static final String TAG = "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; public static ProgressDialog progress;
private static HashMap<String, Bitmap> bitmapHash = new HashMap<>(); private static HashMap<String, Bitmap> bitmapHash = new HashMap<>();
public static final String PHOTOS_DIR_PATH = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/TheSocialNotework/";

View file

@ -1,20 +1,138 @@
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context="com.android_app.matan.ara.sagi.thesocialnotework.SettingsFragment"> tools:context="com.android_app.matan.ara.sagi.thesocialnotework.SettingsFragment">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:padding="10dp"
android:layout_marginTop="15dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.26">
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.65">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/txt_username"
android:hint="Username" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="@+id/txt_password"
android:hint="Password" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/txt_email"
android:hint="Email" />
</LinearLayout>
<RelativeLayout
android:layout_width="90dp"
android:layout_height="90dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/settings_userAvater_iamgeView"
android:layout_gravity="top|center_horizontal"
android:src="@drawable/default_avatar" />
<ImageButton
android:layout_width="45dp"
android:layout_height="45dp"
android:id="@+id/btn_camera"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:background="@android:color/transparent"
android:src="@drawable/ic_menu_camera"
android:tint="#7b7b7b" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0.14"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Nuber Of Notes: "
android:id="@+id/textView3"
android:textStyle="bold"
android:layout_weight="0.25" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="15"
android:id="@+id/lbl_num_of_notes"
android:layout_weight="0.25" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Notes I Liked:"
android:id="@+id/textView4"
android:textStyle="bold"
android:layout_weight="0.25" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="30"
android:id="@+id/lbl_num_of_liked"
android:layout_weight="0.25" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="126dp"
android:layout_gravity="right"
android:layout_weight="0.07">
<Button <Button
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Change" android:text="Save Changes"
android:id="@+id/camera_button" android:id="@+id/btn_save"
android:layout_gravity="center"/> android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="29dp" />
</RelativeLayout>
<ImageView </LinearLayout>
android:layout_width="wrap_content"
android:layout_height="wrap_content" </RelativeLayout>
android:id="@+id/settings_userAvater_iamgeView"
android:layout_gravity="center_horizontal|bottom" />
</FrameLayout>