Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 929e1325 authored by qqq3's avatar qqq3
Browse files

Check api, if api=23 than check permission

parent 02e883ed
Loading
Loading
Loading
Loading
+95 −33
Original line number Diff line number Diff line
package org.asdtm.goodweather;

import android.Manifest;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.NavigationView;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
@@ -414,16 +418,9 @@ public class WeatherPageFragment extends Fragment

        switch (item.getItemId()) {
            case R.id.menu_find_location:
                if (isGPSEnabled) {
                    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                                           0,
                                                           0,
                                                           mLocationListener);
                } else if (isNetworkEnabled){
                    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                                                           0,
                                                           0,
                                                           mLocationListener);
                if (isGPSEnabled && isNetworkEnabled) {
                    gpsRequestLocation();
                    networkRequestLocation();
                } else {
                    showSettingsAlert();
                }
@@ -437,20 +434,38 @@ public class WeatherPageFragment extends Fragment
        @Override
        public void onLocationChanged(Location location)
        {
            mSharedPreferences = getActivity().getSharedPreferences(APP_SETTINGS,
                                                                    Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = mSharedPreferences.edit();

            String latitude = String.format("%1$.2f", location.getLatitude());
            String longitude = String.format("%1$.2f", location.getLongitude());

            editor.putString(APP_SETTINGS_LATITUDE, latitude);
            editor.putString(APP_SETTINGS_LONGITUDE, longitude);
            editor.apply();

            Log.d(TAG, "Current location: " + latitude + ";" + longitude);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (ActivityCompat.checkSelfPermission(getActivity(),
                                                       Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                        getActivity(),
                        Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
            }
            locationManager.removeUpdates(mLocationListener);

            /* isInternetConnection = false;
            connectionDetector = new ConnectionDetector(getContext());
            isInternetConnection = connectionDetector.connectToInternet();*/

            mSharedPreferences = getActivity().getSharedPreferences(APP_SETTINGS,
                                                                    Context.MODE_PRIVATE);
            SharedPreferences.Editor editor = mSharedPreferences.edit();
            editor.putString(APP_SETTINGS_LATITUDE, latitude);
            editor.putString(APP_SETTINGS_LONGITUDE, longitude);
            editor.apply();
        }

        @Override
@@ -484,7 +499,8 @@ public class WeatherPageFragment extends Fragment
                                            @Override
                                            public void onClick(DialogInterface dialog, int which)
                                            {
                    Intent goToSettings = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                                Intent goToSettings = new Intent(
                                                        Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                                                startActivity(goToSettings);
                                            }
                                        });
@@ -501,4 +517,50 @@ public class WeatherPageFragment extends Fragment

        settingsAlert.show();
    }

    public void gpsRequestLocation()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(getActivity(),
                                                   Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                    getActivity(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                                               0,
                                               0,
                                               mLocationListener);
    }

    public void networkRequestLocation()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (ActivityCompat.checkSelfPermission(getActivity(),
                                                   Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
                    getActivity(),
                    Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
                return;
            }
        }
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                                               0,
                                               0,
                                               mLocationListener);
    }
}