From fe1a200589b64d06a13871c84b9ed0bd513ca148 Mon Sep 17 00:00:00 2001 From: Pratik Date: Tue, 3 Nov 2020 13:53:53 +0530 Subject: [PATCH 1/3] Implemented runtime permissions --- .../login/LoginActivity.java | 14 ++++--- .../travel/ListViewRealTimeActivity.java | 42 +++++++++++++++++++ .../travel/MapViewRealTimeActivity.java | 39 +++++++++++++++++ .../utilities/ShareContactActivity.java | 2 + .../app/src/main/java/utils/GPSTracker.java | 11 ++++- .../src/main/res/layout/hotel_listitem.xml | 1 - 6 files changed, 100 insertions(+), 9 deletions(-) diff --git a/Android/app/src/main/java/io/github/project_travel_mate/login/LoginActivity.java b/Android/app/src/main/java/io/github/project_travel_mate/login/LoginActivity.java index a10b149575..2ffe01a9e4 100644 --- a/Android/app/src/main/java/io/github/project_travel_mate/login/LoginActivity.java +++ b/Android/app/src/main/java/io/github/project_travel_mate/login/LoginActivity.java @@ -184,7 +184,8 @@ public void onClick(View view) { String emailString = email_login.getText().toString(); String passString = pass_login.getText().toString(); if (Utils.isNetworkConnected(this)) { - mLoginPresenter.ok_login(emailString, passString, mHandler); + //mLoginPresenter.ok_login(emailString, passString, mHandler); + startMainActivity(); } else { showNoNetwork(); } @@ -303,22 +304,23 @@ public void getRunTimePermissions() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (ContextCompat.checkSelfPermission(LoginActivity.this, Manifest.permission.CAMERA) + != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(LoginActivity.this, + Manifest.permission.READ_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(LoginActivity.this, + Manifest.permission.WRITE_EXTERNAL_STORAGE) + != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(LoginActivity.this, + Manifest.permission.ACCESS_NETWORK_STATE) != PackageManager.PERMISSION_GRANTED) { requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN, - Manifest.permission.READ_CONTACTS, - Manifest.permission.WRITE_CONTACTS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.WAKE_LOCK, Manifest.permission.INTERNET, Manifest.permission.ACCESS_NETWORK_STATE, - Manifest.permission.ACCESS_FINE_LOCATION, - Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.VIBRATE, - }, 0); } } diff --git a/Android/app/src/main/java/io/github/project_travel_mate/travel/ListViewRealTimeActivity.java b/Android/app/src/main/java/io/github/project_travel_mate/travel/ListViewRealTimeActivity.java index 936f3b0414..4a615a49ea 100644 --- a/Android/app/src/main/java/io/github/project_travel_mate/travel/ListViewRealTimeActivity.java +++ b/Android/app/src/main/java/io/github/project_travel_mate/travel/ListViewRealTimeActivity.java @@ -1,16 +1,23 @@ package io.github.project_travel_mate.travel; +import android.Manifest; import android.app.Activity; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; +import android.os.Build; import android.os.Bundle; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.design.widget.Snackbar; import android.support.design.widget.TabLayout; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.view.Menu; import android.view.MenuItem; +import android.widget.Toast; import org.osmdroid.views.MapView; @@ -44,6 +51,41 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { setContentView(R.layout.activity_list_real_time); ButterKnife.bind(this); + getRunTimePermission(); + } + + private void getRunTimePermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED && + ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + getLocation(); + } else { + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION}, 100); + } + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, + @NonNull int[] grantResults) { + if (requestCode != 100) { + return; + } + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED + && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + getLocation(); + } else { + Toast.makeText(this , "Permissions Denied" , Toast.LENGTH_SHORT).show(); + } + } + + public void getLocation() { // Get user's current location mTracker = new GPSTracker(this); MapView mMap = findViewById(R.id.action_map_view); diff --git a/Android/app/src/main/java/io/github/project_travel_mate/travel/MapViewRealTimeActivity.java b/Android/app/src/main/java/io/github/project_travel_mate/travel/MapViewRealTimeActivity.java index 550ee8d446..60de68b361 100644 --- a/Android/app/src/main/java/io/github/project_travel_mate/travel/MapViewRealTimeActivity.java +++ b/Android/app/src/main/java/io/github/project_travel_mate/travel/MapViewRealTimeActivity.java @@ -7,11 +7,14 @@ import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; +import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.os.Looper; import android.preference.PreferenceManager; +import android.support.annotation.NonNull; import android.support.design.widget.Snackbar; +import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; @@ -23,6 +26,7 @@ import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.TextView; +import android.widget.Toast; import com.afollestad.materialdialogs.MaterialDialog; import com.airbnb.lottie.LottieAnimationView; @@ -100,6 +104,41 @@ protected void onCreate(Bundle savedInstanceState) { setTitle("Map View"); + getRunTimePermission(); + } + + private void getRunTimePermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED && + ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + getLocation(); + } else { + ActivityCompat.requestPermissions(this, + new String[]{Manifest.permission.ACCESS_FINE_LOCATION, + Manifest.permission.ACCESS_COARSE_LOCATION}, 100); + } + } + } + + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, + @NonNull int[] grantResults) { + if (requestCode != 100) { + return; + } + if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED + && ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { + getLocation(); + } else { + Toast.makeText(this, "Permissions Denied", Toast.LENGTH_SHORT).show(); + } + } + + private void getLocation() { // Get user's current location tracker = new GPSTracker(this); if (!tracker.canGetLocation()) { diff --git a/Android/app/src/main/java/io/github/project_travel_mate/utilities/ShareContactActivity.java b/Android/app/src/main/java/io/github/project_travel_mate/utilities/ShareContactActivity.java index c86ced430f..65c9afa9f0 100644 --- a/Android/app/src/main/java/io/github/project_travel_mate/utilities/ShareContactActivity.java +++ b/Android/app/src/main/java/io/github/project_travel_mate/utilities/ShareContactActivity.java @@ -55,6 +55,8 @@ protected void onCreate(Bundle savedInstanceState) { ButterKnife.bind(this); + + mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); createCode(); scan.setOnClickListener(this); diff --git a/Android/app/src/main/java/utils/GPSTracker.java b/Android/app/src/main/java/utils/GPSTracker.java index e4587fbcaa..572efecb78 100644 --- a/Android/app/src/main/java/utils/GPSTracker.java +++ b/Android/app/src/main/java/utils/GPSTracker.java @@ -1,13 +1,16 @@ package utils; +import android.Manifest; import android.app.Service; import android.content.Context; import android.content.Intent; +import android.content.pm.PackageManager; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.os.Bundle; import android.os.IBinder; +import android.support.v4.app.ActivityCompat; import android.util.Log; import org.osmdroid.views.MapView; @@ -56,7 +59,10 @@ private Location getLocation() { if (!isGPSEnabled && !isNetworkEnabled) { // no network provider is enabled - } else { + } else if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) + == PackageManager.PERMISSION_GRANTED + && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) + == PackageManager.PERMISSION_GRANTED) { this.mCanGetLocation = true; // First get mLocation from Network Provider if (isNetworkEnabled) { @@ -156,4 +162,5 @@ public void displayLocationRequest(Context context, MapView mapView) { mLocationOverlay.enableMyLocation(); mapView.getOverlays().add(mLocationOverlay); } -} \ No newline at end of file +} + diff --git a/Android/app/src/main/res/layout/hotel_listitem.xml b/Android/app/src/main/res/layout/hotel_listitem.xml index d72be0d06b..3256529e21 100755 --- a/Android/app/src/main/res/layout/hotel_listitem.xml +++ b/Android/app/src/main/res/layout/hotel_listitem.xml @@ -26,7 +26,6 @@ android:layout_width="match_parent" android:layout_height="70dp" android:layout_alignParentStart="true" - android:layout_alignParentTop="true" app:srcCompat="@color/colorAccent" /> Date: Tue, 3 Nov 2020 14:11:00 +0530 Subject: [PATCH 2/3] Implemeted runtime permissions --- .../io/github/project_travel_mate/login/LoginActivity.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Android/app/src/main/java/io/github/project_travel_mate/login/LoginActivity.java b/Android/app/src/main/java/io/github/project_travel_mate/login/LoginActivity.java index 2ffe01a9e4..cdb41a8855 100644 --- a/Android/app/src/main/java/io/github/project_travel_mate/login/LoginActivity.java +++ b/Android/app/src/main/java/io/github/project_travel_mate/login/LoginActivity.java @@ -184,8 +184,7 @@ public void onClick(View view) { String emailString = email_login.getText().toString(); String passString = pass_login.getText().toString(); if (Utils.isNetworkConnected(this)) { - //mLoginPresenter.ok_login(emailString, passString, mHandler); - startMainActivity(); + mLoginPresenter.ok_login(emailString, passString, mHandler); } else { showNoNetwork(); } From ff09550c1826e97ceb0e780565c94e93f93170fc Mon Sep 17 00:00:00 2001 From: Pratik Ghodke <72707876+PratikGhodke@users.noreply.github.com> Date: Tue, 3 Nov 2020 22:51:51 +0530 Subject: [PATCH 3/3] Update ShareContactActivity.java --- .../project_travel_mate/utilities/ShareContactActivity.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/Android/app/src/main/java/io/github/project_travel_mate/utilities/ShareContactActivity.java b/Android/app/src/main/java/io/github/project_travel_mate/utilities/ShareContactActivity.java index 65c9afa9f0..c86ced430f 100644 --- a/Android/app/src/main/java/io/github/project_travel_mate/utilities/ShareContactActivity.java +++ b/Android/app/src/main/java/io/github/project_travel_mate/utilities/ShareContactActivity.java @@ -55,8 +55,6 @@ protected void onCreate(Bundle savedInstanceState) { ButterKnife.bind(this); - - mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); createCode(); scan.setOnClickListener(this);