From 92138f26bb74a3d2c85b0121424ec4ebdd2e98b2 Mon Sep 17 00:00:00 2001 From: Aran Zaiger Date: Sat, 9 Jul 2016 02:24:32 +0300 Subject: [PATCH] added permissions checks and handling --- .../ara/sagi/thesocialnotework/GPSUtils.java | 60 ++++++++-------- .../sagi/thesocialnotework/GmapFragment.java | 2 +- .../sagi/thesocialnotework/LoginActivity.java | 44 ++++++++++++ .../sagi/thesocialnotework/MainActivity.java | 71 +++++++++++++------ .../thesocialnotework/PersonalFragment.java | 27 ++++--- .../thesocialnotework/SettingsFragment.java | 18 ++++- .../ara/sagi/thesocialnotework/Utils.java | 23 +++++- 7 files changed, 178 insertions(+), 67 deletions(-) diff --git a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GPSUtils.java b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GPSUtils.java index 6188eb1..5f81794 100644 --- a/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GPSUtils.java +++ b/app/src/main/java/com/android_app/matan/ara/sagi/thesocialnotework/GPSUtils.java @@ -27,7 +27,7 @@ import java.util.List; */ public class GPSUtils extends Service implements LocationListener { - private static final String TAG ="GPSUtils" ; + private static final String TAG = "GPSUtils"; private final Context mContext; // flag for GPS status @@ -53,7 +53,7 @@ public class GPSUtils extends Service implements LocationListener { getLocation(); } -// public Location getLocation() { + // public Location getLocation() { // LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); // if (locationManager != null) { // Location lastKnownLocationGPS = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER); @@ -69,14 +69,14 @@ public class GPSUtils extends Service implements LocationListener { // return null; // } public Location getLocation() { - try { - Log.d("TAG","in get location"); - locationManager = - (LocationManager) mContext - .getSystemService(LOCATION_SERVICE); + try { + Log.d("TAG", "in get location"); + locationManager = + (LocationManager) mContext + .getSystemService(LOCATION_SERVICE); - // getting GPS status - isGPSEnabled = locationManager + // getting GPS status + isGPSEnabled = locationManager .isProviderEnabled(LocationManager.GPS_PROVIDER); // getting network status @@ -91,10 +91,10 @@ public class GPSUtils extends Service implements LocationListener { if (isNetworkEnabled) { if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { Log.e("TAG", "No Permissions"); - ((MainActivity)mContext).setLocationPermission(false); + Utils.setLocationPermission(false); return null; - }else{ - ((MainActivity)mContext).setLocationPermission(true); + } else { + Utils.setLocationPermission(true); } Log.d(TAG, "Network"); if (locationManager != null) { @@ -120,9 +120,8 @@ public class GPSUtils extends Service implements LocationListener { } } } - } - else{ - location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); + } else { + location = locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER); } } @@ -133,7 +132,7 @@ public class GPSUtils extends Service implements LocationListener { return location; } - public void showSettingsAlert(){ + public void showSettingsAlert() { AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext); // Setting Dialog Title @@ -144,7 +143,7 @@ public class GPSUtils extends Service implements LocationListener { // On pressing Settings button alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog,int which) { + public void onClick(DialogInterface dialog, int which) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); mContext.startActivity(intent); } @@ -188,13 +187,13 @@ public class GPSUtils extends Service implements LocationListener { return null; } - public List
getFromLocation(double latitude, double longitude, int maxResults){ + public List
getFromLocation(double latitude, double longitude, int maxResults) { try { List
addresses; geocoder = new Geocoder(mContext); if (latitude != 0 || longitude != 0) { - addresses = geocoder.getFromLocation(latitude , + addresses = geocoder.getFromLocation(latitude, longitude, maxResults); return addresses; @@ -212,11 +211,11 @@ public class GPSUtils extends Service implements LocationListener { /** * Function to get latitude - * */ - public double getLatitude(){ - if(location != null){ + */ + public double getLatitude() { + if (location != null) { latitude = location.getLatitude(); - }else{ + } else { Log.d("Tag", "Got NULL"); } @@ -226,9 +225,9 @@ public class GPSUtils extends Service implements LocationListener { /** * Function to get longitude - * */ - public double getLongitude(){ - if(location != null){ + */ + public double getLongitude() { + if (location != null) { longitude = location.getLongitude(); } @@ -236,21 +235,20 @@ public class GPSUtils extends Service implements LocationListener { return longitude; } - public Location getLastKnownLocation(){ + public Location getLastKnownLocation() { if (location == null) return getLocation(); return location; } public String getAddress() { - String address=""; + String address = ""; - - List
listAddress = getFromLocation(getLatitude(),getLongitude(),1); + List
listAddress = getFromLocation(getLatitude(), getLongitude(), 1); for (int i = 0; i <= listAddress.get(0).getMaxAddressLineIndex(); i++) { listAddress.get(0).getAddressLine(i); - address += listAddress.get(0).getAddressLine(i)+" "; + address += listAddress.get(0).getAddressLine(i) + " "; } return address; } 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 1774054..6eb62b0 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 @@ -315,7 +315,7 @@ public class GmapFragment extends Fragment implements OnMapReadyCallback { } } }; - + private class getMarkersFromNotes extends AsyncTask, MarkerNoteStruct, Void> { 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 3d260c5..5ff7851 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 @@ -3,6 +3,8 @@ package com.android_app.matan.ara.sagi.thesocialnotework; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.text.Editable; @@ -40,6 +42,8 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList private final String LOGIN_PATH = "/login"; private LoginActivity self; //this protected LinearLayout layout; + private boolean permissionsReturend = false; + @Override protected void onCreate(Bundle savedInstanceState) { @@ -59,6 +63,9 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList this.mLoginButton.setOnClickListener(this); this.mLoginButton.setEnabled(false); + //check for permission +// while(!permissionsReturend); + } private void removeFocuse() { @@ -220,5 +227,42 @@ public class LoginActivity extends AppCompatActivity implements View.OnClickList public void afterTextChanged(Editable editable) { } +// @Override +// public void onRequestPermissionsResult(int requestCode, +// String permissions[], int[] grantResults) { +// permissionsReturend = true; +// Log.d(TAG, "onRequestPermissionsResult: in func"); +// switch (requestCode) { +// case FINE_PERM: { +// // If request is cancelled, the result arrays are empty. +// if (grantResults.length > 0 +// && grantResults[0] == PackageManager.PERMISSION_GRANTED) { +// Log.d(TAG, "onRequestPermissionsResult: got permission for location"); +// Utils.setLocationPermission(true); +// ActivityCompat.requestPermissions(LoginActivity.this, new String[]{android.Manifest.permission.CAMERA}, CAMERA_PERM); +// } else { +// Log.d(TAG, "onRequestPermissionsResult:DIDNT get permission for location"); +// +// Toast.makeText(LoginActivity.this,"No Location Permissions granted.\n\"An App is nothing without its permissions\"",Toast.LENGTH_LONG); +//// System.exit(0); +// } +// return; +// } +// case CAMERA_PERM: { +// if (grantResults.length > 0 +// && grantResults[0] == PackageManager.PERMISSION_GRANTED) { +// Log.d(TAG, "onRequestPermissionsResult: got permission for camera"); +// Utils.setCameraPermission(true); +// +// +// } else { +// Log.d(TAG, "onRequestPermissionsResult: DIDNT get permission for camera"); +// Toast.makeText(LoginActivity.this,"No Camera Permissions granted.\n\"An App is nothing without its permissions\"",Toast.LENGTH_LONG); +// } +// return; +// } +// } +// } + } 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..b34b561 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 @@ -1,9 +1,11 @@ package com.android_app.matan.ara.sagi.thesocialnotework; +import android.*; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; @@ -11,6 +13,7 @@ import android.os.Bundle; import android.os.StrictMode; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; +import android.support.v4.app.ActivityCompat; import android.support.v4.app.FragmentTransaction; import android.util.Log; import android.view.LayoutInflater; @@ -24,6 +27,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; @@ -44,15 +48,15 @@ public class MainActivity extends AppCompatActivity protected final String TAG = "[TSN / MainActivity]"; protected User user; private GPSUtils gpsUtils; - private boolean locationPermission; private GmapFragment gmapFragment; private PersonalFragment personalFragment; private SettingsFragment settingsFragment; private Toolbar toolbar; public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api"; private ImageView menu_avatar; - private MainActivity self; - private NavigationView nav_view; + private MainActivity self; + private NavigationView nav_view; + private final int FINE_PERM = 0, CAMERA_PERM = 1; @Override @@ -60,6 +64,8 @@ public class MainActivity extends AppCompatActivity super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); this.self = this; + + toolbar = (Toolbar) findViewById(R.id.toolbar); toolbar.setTitle("Personal Notes"); setSupportActionBar(toolbar); @@ -69,11 +75,6 @@ public class MainActivity extends AppCompatActivity StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); - - - - - DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); @@ -86,12 +87,12 @@ public class MainActivity extends AppCompatActivity //get Bundle data (UserString) Bundle b = getIntent().getExtras(); - this.user = new User(b.getString("UserData")); + this.user = new User(b.getString("UserData")); //Get The Nav_View Avatar View nav_view = (NavigationView) findViewById(R.id.nav_view); View header_v = nav_view.getHeaderView(0); - menu_avatar = (ImageView)header_v.findViewById(R.id.nav_user_avatar); + menu_avatar = (ImageView) header_v.findViewById(R.id.nav_user_avatar); //Change Layout @@ -106,7 +107,7 @@ public class MainActivity extends AppCompatActivity gpsUtils.getLocation(); //Change The Avatar - Utils.URLtoImageView(menu_avatar, user.getAvatar()); + Utils.URLtoImageView(menu_avatar, user.getAvatar()); } @Override @@ -154,14 +155,14 @@ public class MainActivity extends AppCompatActivity setSupportActionBar(toolbar); } else if (id == R.id.nav_map) { - Log.d(TAG,"Before going to map"); + Log.d(TAG, "Before going to map"); toolbar.setTitle("Map"); setSupportActionBar(toolbar); ft.replace(R.id.fragment_container, gmapFragment); ft.commit(); } else if (id == R.id.nav_personal) { - Log.d(TAG,"Before going to personal"); + Log.d(TAG, "Before going to personal"); ft.replace(R.id.fragment_container, personalFragment); ft.commit(); } else if (id == R.id.nav_settings) { @@ -189,20 +190,44 @@ public class MainActivity extends AppCompatActivity return this.gpsUtils; } - public void setLocationPermission(boolean locationPermission) { - this.locationPermission = locationPermission; + + public User getUser() { + return user; } + public String getUserId() { + return user.getId(); + } + @Override + public void onRequestPermissionsResult(int requestCode, + String permissions[], int[] grantResults) { + Log.d(TAG, "onRequestPermissionsResult: in func"); + switch (requestCode) { + case FINE_PERM: { + // If request is cancelled, the result arrays are empty. + if (grantResults.length > 1) { + if (!(grantResults[0] == PackageManager.PERMISSION_GRANTED)){ + Log.d(TAG, "onRequestPermissionsResult: Did Not get permission for location"); + Toast.makeText(MainActivity.this, "No Location Permissions granted.\n\"An App is nothing without its permissions\"", Toast.LENGTH_LONG).show(); + System.exit(0); + } + if (!(grantResults[1] == PackageManager.PERMISSION_GRANTED)) { + Log.d(TAG, "onRequestPermissionsResult:DIDNT get permission for camera"); + Toast.makeText(MainActivity.this, "No Camera Permissions granted.\nyou will not be able to change avatar", Toast.LENGTH_LONG).show(); + Utils.setCameraPermission(false); + } else { + Utils.setCameraPermission(true); + } + return; + } + } + } - - - public User getUser(){ - return user; - } - - public String getUserId(){return user.getId();} - + } } + + + 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 53c49a3..9e30c96 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 @@ -51,7 +51,6 @@ import java.util.List; public class PersonalFragment extends Fragment { protected ListView noteList; - private final int FINE_PERM = 0; private final String BASE_URL = "http://thesocialnotework-api.appspot.com/api"; private GPSUtils gpsUtils; private List listOfNotes; @@ -59,6 +58,8 @@ public class PersonalFragment extends Fragment { private String userId; private final String TAG = "[TSN/PersonalFragment]"; private MainActivity activity; + private final int FINE_PERM = 0, CAMERA_PERM = 1; + public PersonalFragment() { // Required empty public constructor @@ -74,8 +75,18 @@ public class PersonalFragment extends Fragment { Bundle bundle = getArguments(); this.userId = activity.getUserId(); Log.d(TAG, "onCreateView: userID: " + userId); - //check for permission - ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, FINE_PERM); + + ActivityCompat.requestPermissions(activity, new String[]{ + android.Manifest.permission.ACCESS_FINE_LOCATION, + android.Manifest.permission.CAMERA + }, + FINE_PERM + ); +// ActivityCompat.requestPermissions(activity, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, FINE_PERM); + +// if (!Utils.arePermissionsGranted()) { +// Toast.makeText(getContext(), "Missing some Permissions...\nPlease go to app info and enable them", Toast.LENGTH_LONG).show(); +// } this.noteList = (ListView) view.findViewById(R.id.ps_list_listview); @@ -287,10 +298,7 @@ public class PersonalFragment extends Fragment { } - - - - private void addNoteFromJsonObj(JSONObject noteObject, Date time) throws JSONException { + private void addNoteFromJsonObj(JSONObject noteObject, Date time) throws JSONException { Note addNote = new Note( noteObject.getString("id"), Float.parseFloat(noteObject.getJSONObject("location").getString("lat")), @@ -344,7 +352,7 @@ public class PersonalFragment extends Fragment { time.setText(note.getTimestamp()); location.setText("Address: " + note.getAddress()); likes.setText("Likes: " + note.getLikes()); - tags.setText("Tags: "+ note.getTags().toString()); + tags.setText("Tags: " + note.getTags().toString()); permission.setText("Permission: " + (note.isPublic() ? "Public" : "Private")); Utils.URLtoImageView(avatar, note.getAvatar()); @@ -394,4 +402,7 @@ public class PersonalFragment extends Fragment { } + + + } 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..b77b215 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,12 +1,16 @@ package com.android_app.matan.ara.sagi.thesocialnotework; +import android.*; +import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.graphics.Bitmap; 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.util.Log; import android.view.LayoutInflater; @@ -14,6 +18,7 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.ImageView; +import android.widget.Toast; public class SettingsFragment extends Fragment { private static final String ARG_PARAM1 = "param1"; @@ -105,10 +110,17 @@ public class SettingsFragment extends Fragment { public void onClick(View v) { + if (ActivityCompat.checkSelfPermission((MainActivity) getActivity(), + Manifest.permission.CAMERA) + == PackageManager.PERMISSION_GRANTED) { + Log.d(TAG, "in camera Button"); + Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); + startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); + } + else{ + Toast.makeText(getActivity(), "No Location Permissions granted.\n\"An App is nothing without its permissions\"", Toast.LENGTH_LONG).show(); - Log.d(TAG, "in camera Button"); - Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); - startActivityForResult(intent, REQUEST_IMAGE_CAPTURE); + } } 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..691f22b 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 @@ -34,7 +34,8 @@ public class Utils { public static final String BASE_URL = "http://thesocialnotework-api.appspot.com/api"; public static ProgressDialog progress; private static HashMap bitmapHash = new HashMap<>(); - + private static boolean mLocationPermission = false; + private static boolean mCameraPermission = false; public static Bitmap getBitmapFromURL(String url) { @@ -156,4 +157,24 @@ public class Utils { } + + public static void setLocationPermission(boolean locationPermission) { + mLocationPermission = locationPermission; + } + public static void setCameraPermission(boolean cameraPermission) { + mCameraPermission = cameraPermission; + } + public static boolean arePermissionsGranted() { + return (mLocationPermission && mCameraPermission); + } + + public static boolean isCameraPermissionGranted(){ + return mCameraPermission; + } + public static boolean isLocationPermissionGranted(){ + return mLocationPermission; + } + + + }