From 49faa9a8e2f6e70cfb0e77c67602e817f3bf2e38 Mon Sep 17 00:00:00 2001 From: Aran Zaiger Date: Fri, 8 Jul 2016 00:06:09 +0300 Subject: [PATCH] created Utils class migrated functions used all over the app to Utils --- .../sagi/thesocialnotework/GmapFragment.java | 32 ++--- .../sagi/thesocialnotework/LoginActivity.java | 10 +- .../sagi/thesocialnotework/MainActivity.java | 56 --------- .../thesocialnotework/PersonalFragment.java | 62 +++++----- .../thesocialnotework/RegisterActivity.java | 8 +- .../ara/sagi/thesocialnotework/Utils.java | 110 ++++++++++++++++++ 6 files changed, 159 insertions(+), 119 deletions(-) create mode 100644 app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java index 7adea11..5390b8b 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GmapFragment.java @@ -50,6 +50,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { private GoogleMap mMap; private GPSUtils gpsUtils; private MainActivity mainActivity; + private final int MAX_ZOOM = 16, MIN_ZOOM = 9; public GmapFragment() {} @@ -110,17 +111,17 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { mMap.setOnCameraChangeListener(new GoogleMap.OnCameraChangeListener() { @Override public void onCameraChange(CameraPosition cameraPosition) { - if (cameraPosition.zoom > 14) { - getMap().animateCamera(CameraUpdateFactory.zoomTo(14)); + if (cameraPosition.zoom > MAX_ZOOM) { + getMap().animateCamera(CameraUpdateFactory.zoomTo(MAX_ZOOM)); } - if (cameraPosition.zoom < 8) { - getMap().animateCamera(CameraUpdateFactory.zoomTo(8)); + if (cameraPosition.zoom < MIN_ZOOM) { + getMap().animateCamera(CameraUpdateFactory.zoomTo(MIN_ZOOM)); } } }); - VolleyUtilSingleton.getInstance(getActivity()).get(mainActivity.BASE_URL + "/note/all?uid=" + mainActivity.getUserId(), getNotesSuccessListener, mainActivity.genericErrorListener); + VolleyUtilSingleton.getInstance(getActivity()).get(Utils.BASE_URL + "/note/all?uid=" + mainActivity.getUserId(), getNotesSuccessListener, Utils.genericErrorListener); // VolleyUtilSingleton.getInstance(getActivity()).get(mainActivity.BASE_URL + "/note/all?uid=" + mainActivity.getUserId(), getNotesSuccessListener, mainActivity.genericErrorListener); LatLng userLocation = new LatLng(gpsUtils.getLatitude(), gpsUtils.getLongitude()); // mMap.addMarker(new MarkerOptions().position(userLocation).title("I Am Here!")); @@ -146,7 +147,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { for (int i = 0; i < noteObjectsArray.length(); i++) { JSONObject noteObject = noteObjectsArray.getJSONObject(i); time.setTime(noteObject.getLong("created_at")); - listOfNotes.add(mainActivity.getNoteFromJsonObj(noteObject, time)); + listOfNotes.add(Utils.getNoteFromJsonObj(noteObject, time)); } new getMarkersFromNotes(mMap).execute(listOfNotes); // noteList.setAdapter(noteListAdapter); @@ -159,22 +160,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { - public Bitmap getBitmapFromURL(String imageUrl) { - try { - URL url = new URL(imageUrl); - HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - connection.setDoInput(true); - connection.connect(); - InputStream input = connection.getInputStream(); - Bitmap myBitmap = BitmapFactory.decodeStream(input); - Log.d(TAG, "image: " + myBitmap.toString()); - return Bitmap.createScaledBitmap(myBitmap, 80, 80, false); - } catch (IOException e) { - e.printStackTrace(); - return null; - } - } private class getMarkersFromNotes extends AsyncTask, MarkerOptions, List> { @@ -215,7 +201,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { .title(n.getTitle()) .position(new LatLng(n.getLat(), n.getLon())) .snippet(n.getBody()) - .icon(BitmapDescriptorFactory.fromBitmap(getBitmapFromURL(url))); + .icon(BitmapDescriptorFactory.fromBitmap(Bitmap.createScaledBitmap(Utils.getBitmapFromURL(url),80,80,false))); publishProgress(mo); // ); @@ -224,7 +210,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { } - +//Bitmap.createScaledBitmap(myBitmap, 80, 80, false); } diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java index bb0879a..2769a1b 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/LoginActivity.java @@ -82,7 +82,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList } } private void attemptLogin() { - MainActivity.showLoadingDialog(this, "Connecting", "Authenticating data"); + Utils.showLoadingDialog(this, "Connecting", "Authenticating data"); mPasswordView.setError(null); if (isParamsValid(mUsernameView.getText().toString(), mPasswordView.getText().toString())) { @@ -100,7 +100,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList } VolleyUtilSingleton.getInstance(LoginActivity.this).post(BASE_URL + LOGIN_PATH, tempJson, onLoginSuccess, onLoginError); } else { - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); Log.d(TAG, "Invalid params - make sure username exist & password is 4 characters or more"); } @@ -120,10 +120,10 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList Bundle loginUserBundle = new Bundle(); loginUserBundle.putString("user_id", response.getJSONObject("user").getString("id")); personalSpaceActivity.putExtras(loginUserBundle); - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); startActivity(personalSpaceActivity); } else { - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); Toast.makeText(self, "Username or Password are incorrect", Toast.LENGTH_LONG).show(); self.mUsernameView.getText().clear(); self.mPasswordView.getText().clear(); @@ -141,7 +141,7 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList Response.ErrorListener onLoginError = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); Toast.makeText(self , "Username Or Password Incorrect", Toast.LENGTH_LONG).show(); //Clean texts self.mUsernameView.getText().clear(); 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 4f98e12..327c5a2 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 @@ -38,7 +38,6 @@ public class MainActivity extends AppCompatActivity protected String userId; private GPSUtils gpsUtils; private boolean locationPermission; - public static ProgressDialog progress; private GmapFragment gmapFragment; private PersonalFragment personalFragment; private Toolbar toolbar; @@ -172,62 +171,7 @@ public class MainActivity extends AppCompatActivity - public static void showLoadingDialog(Context context, String title, String msg) { - progress = new ProgressDialog(context); - progress.setTitle(title); - progress.setMessage(msg); - progress.setCanceledOnTouchOutside(false); - progress.show(); - } - public static void dismissLoadingDialog() { - - if (progress != null && progress.isShowing()) { - progress.dismiss(); - } - } - - // //Generic response ErrorListener - Response.ErrorListener genericErrorListener = new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Log.d(TAG, "genericErrorListener"); - error.printStackTrace(); - } - }; - - public Note getNoteFromJsonObj(JSONObject noteObject, Date time) throws JSONException { -// List listOfNotes = new ArrayList<>(); - - Note note = new Note( - noteObject.getString("id"), - Float.parseFloat(noteObject.getJSONObject("location").getString("lat")), - Float.parseFloat(noteObject.getJSONObject("location").getString("lng")), - noteObject.getJSONObject("location").getString("address"), - noteObject.getString("title"), - noteObject.getString("body"), - time.toString(), - noteObject.getBoolean("is_public"), - noteObject.getInt("likes"), - jsonArrayToStringArray(noteObject.getJSONArray("tags")) - ); - return note; -// listOfNotes.add(addNote); - - } - - public ArrayList jsonArrayToStringArray(JSONArray jArray) { - ArrayList stringArray = new ArrayList(); - for (int i = 0, count = jArray.length(); i < count; i++) { - try { - JSONObject jsonObject = jArray.getJSONObject(i); - stringArray.add(jsonObject.toString()); - } catch (JSONException e) { - e.printStackTrace(); - } - } - return stringArray; - } public String getUserId(){return userId;} diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalFragment.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalFragment.java index 3a7b172..28fbbda 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalFragment.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/PersonalFragment.java @@ -80,7 +80,7 @@ public class PersonalFragment extends Fragment { noteListAdapter = new ListAdapter(getContext(), listOfNotes); noteList.setAdapter(noteListAdapter); noteList.setOnItemClickListener(new ItemClickedListener()); - MainActivity.showLoadingDialog(getActivity(), "Fetching..", "getting your notes"); + Utils.showLoadingDialog(getActivity(), "Fetching..", "getting your notes"); getAllNotes(); //https://thesocialnotework-api.appspot.com/api/note/all?uid= @@ -95,19 +95,19 @@ public class PersonalFragment extends Fragment { @Override public void onAttach(Context context) { super.onAttach(context); - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); Log.d(TAG, "onAttach"); } @Override public void onDetach() { super.onDetach(); - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); } public void getAllNotes() { Log.d(TAG, "url: " + BASE_URL + "/note/all?uid=" + userId); - VolleyUtilSingleton.getInstance(getActivity()).get(BASE_URL + "/note/all?uid=" + userId, getNotesSuccessListener, genericErrorListener); + VolleyUtilSingleton.getInstance(getActivity()).get(BASE_URL + "/note/all?uid=" + userId, getNotesSuccessListener, Utils.genericErrorListener); } private View.OnClickListener addNewNoteDialog = new View.OnClickListener() { @@ -176,7 +176,7 @@ public class PersonalFragment extends Fragment { } //send request and close dialog - VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/upsert", noteJson, newNoteSuccessListener, genericErrorListener); + VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/upsert", noteJson, newNoteSuccessListener, Utils.genericErrorListener); dialog.dismiss(); } }); @@ -214,13 +214,13 @@ public class PersonalFragment extends Fragment { }; - //response Error listener for adding new note - Response.ErrorListener newNoteErrorListener = new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Log.d(TAG, "newNoteError: msg: " + error.getMessage()); - } - }; +// //response Error listener for adding new note +// Response.ErrorListener newNoteErrorListener = new Response.ErrorListener() { +// @Override +// public void onErrorResponse(VolleyError error) { +// Log.d(TAG, "newNoteError: msg: " + error.getMessage()); +// } +// }; //response listener for getting all user notes @@ -228,7 +228,7 @@ public class PersonalFragment extends Fragment { @Override public void onResponse(JSONObject response) { Log.d(TAG, "getNotesSuccessListener: " + response.toString()); - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); try { //need to get all notes and add to listOfNotes JSONArray noteObjectsArray = response.getJSONArray("notes"); @@ -248,24 +248,24 @@ public class PersonalFragment extends Fragment { }; - //response ErrorListener for getting all user notes - Response.ErrorListener getNotesErrorListener = new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Log.d(TAG, "getNotesErrorListener: " + error.getMessage()); - MainActivity.dismissLoadingDialog(); - } - }; +// //response ErrorListener for getting all user notes +// Response.ErrorListener getNotesErrorListener = new Response.ErrorListener() { +// @Override +// public void onErrorResponse(VolleyError error) { +// Log.d(TAG, "getNotesErrorListener: " + error.getMessage()); +// MainActivity.dismissLoadingDialog(); +// } +// }; - //Generic response ErrorListener - Response.ErrorListener genericErrorListener = new Response.ErrorListener() { - @Override - public void onErrorResponse(VolleyError error) { - Log.d(TAG, "genericErrorListener"); - MainActivity.dismissLoadingDialog(); - error.printStackTrace(); - } - }; +// //Generic response ErrorListener +// Response.ErrorListener genericErrorListener = new Response.ErrorListener() { +// @Override +// public void onErrorResponse(VolleyError error) { +// Log.d(TAG, "genericErrorListener"); +// MainActivity.dismissLoadingDialog(); +// error.printStackTrace(); +// } +// }; private ArrayList jsonArrayToStringArray(JSONArray jArray) { @@ -360,7 +360,7 @@ public class PersonalFragment extends Fragment { try { delNote.put("uid", userId); delNote.put("nid", note.getId()); - VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/delete", delNote, deleteNoteSuccessListener, genericErrorListener); + VolleyUtilSingleton.getInstance(getActivity()).post(BASE_URL + "/note/delete", delNote, deleteNoteSuccessListener, Utils.genericErrorListener); listOfNotes.remove(position); } catch (JSONException e) { diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/RegisterActivity.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/RegisterActivity.java index c5327c4..b74bc02 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/RegisterActivity.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/RegisterActivity.java @@ -75,7 +75,7 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL } private void attemptRegister() { - MainActivity.showLoadingDialog(this, "Registering", "Please Wait..."); + Utils.showLoadingDialog(this, "Registering", "Please Wait..."); Log.d(TAG, "in attemptRegister: Registering.."); if (isParamsValid(mUsernameView.getText().toString(), mPasswordView.getText().toString(), mEmailView.getText().toString())) { Log.d(TAG, "params are valid"); @@ -94,7 +94,7 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL Log.d(TAG,"JSON: "+tempJson.toString()); VolleyUtilSingleton.getInstance(RegisterActivity.this).post(BASE_URL + REG_PATH, tempJson, onRegisterSuccess, onRegisterError); } else { - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); Log.d(TAG, "Invalid params - make sure username exist, password is 4 characters or more & email is valid"); Toast.makeText(this, "Make Sure tou have entered a valid email. password at least 4 chars", Toast.LENGTH_LONG).show(); } @@ -104,7 +104,7 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL @Override public void onResponse(JSONObject response) { Log.d(TAG,"response: "+ response.toString()); - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); try { if(response.getString("message").equals("created")) { Log.d(TAG, "onRegisterSuccess => user created"); // TODO: REMOVE console @@ -125,7 +125,7 @@ public class RegisterActivity extends AppCompatActivity implements View.OnClickL Response.ErrorListener onRegisterError = new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { - MainActivity.dismissLoadingDialog(); + Utils.dismissLoadingDialog(); Toast.makeText(self , "Username is already taken. maybe: " + mUsernameView.getText().toString()+"_666 ? :)", Toast.LENGTH_LONG).show(); Log.d(TAG, "onRegisterError: msg: " + error.getMessage()); } 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 new file mode 100644 index 0000000..afe431e --- /dev/null +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/Utils.java @@ -0,0 +1,110 @@ +package com.android_app.matan.ara.sagi.thesocialnotework; + +import android.app.ProgressDialog; +import android.content.Context; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.util.Log; + +import com.android.volley.Response; +import com.android.volley.VolleyError; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import java.util.ArrayList; +import java.util.Date; + +/** + * Created by JERLocal on 7/7/2016. + */ +public class Utils { + + public static final String TAG = "Utils"; + public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api"; + public static ProgressDialog progress; + + + + + public static Bitmap getBitmapFromURL(String imageUrl) { + + try { + URL url = new URL(imageUrl); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); + connection.setDoInput(true); + connection.connect(); + InputStream input = connection.getInputStream(); + Bitmap myBitmap = BitmapFactory.decodeStream(input); + return myBitmap; + } catch (IOException e) { + e.printStackTrace(); + return null; + } + } + + + // //Generic response ErrorListener + public static Response.ErrorListener genericErrorListener = new Response.ErrorListener() { + @Override + public void onErrorResponse(VolleyError error) { + Log.d(TAG, "genericErrorListener"); + error.printStackTrace(); + } + }; + + public static void showLoadingDialog(Context context, String title, String msg) { + progress = new ProgressDialog(context); + progress.setTitle(title); + progress.setMessage(msg); + progress.setCanceledOnTouchOutside(false); + progress.show(); + } + + public static void dismissLoadingDialog() { + + if (progress != null && progress.isShowing()) { + progress.dismiss(); + } + } + + + + public static Note getNoteFromJsonObj(JSONObject noteObject, Date time) throws JSONException { +// List listOfNotes = new ArrayList<>(); + + Note note = new Note( + noteObject.getString("id"), + Float.parseFloat(noteObject.getJSONObject("location").getString("lat")), + Float.parseFloat(noteObject.getJSONObject("location").getString("lng")), + noteObject.getJSONObject("location").getString("address"), + noteObject.getString("title"), + noteObject.getString("body"), + time.toString(), + noteObject.getBoolean("is_public"), + noteObject.getInt("likes"), + jsonArrayToStringArray(noteObject.getJSONArray("tags")) + ); + return note; +// listOfNotes.add(addNote); + + } + + public static ArrayList jsonArrayToStringArray(JSONArray jArray) { + ArrayList stringArray = new ArrayList(); + for (int i = 0, count = jArray.length(); i < count; i++) { + try { + JSONObject jsonObject = jArray.getJSONObject(i); + stringArray.add(jsonObject.toString()); + } catch (JSONException e) { + e.printStackTrace(); + } + } + return stringArray; + } +}