0

I'm creating an app with android studio and I'm using the Google Fit app to sync the two to get some measurements through Mi-Fit. problem is that the getting permissions window isn't popping in my phone and I can't figure out why. this is my Permission class:

package Model.Users;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInOptionsExtension;
import com.google.android.gms.fitness.FitnessOptions;

import static com.google.android.gms.fitness.data.DataType.TYPE_ACTIVITY_SEGMENT;
import static com.google.android.gms.fitness.data.DataType.TYPE_CALORIES_EXPENDED;
import static com.google.android.gms.fitness.data.DataType.TYPE_DISTANCE_DELTA;
import static com.google.android.gms.fitness.data.DataType.TYPE_STEP_COUNT_DELTA;

public class Permissions {

    private AppCompatActivity app;
    final private int ALL_PERMISSIONS = 101;

    public Permissions(AppCompatActivity app) {
        this.app = app;
    }

    /**
     *
     * @throws PackageManager.NameNotFoundException
     */
    public void requestPermissions() throws PackageManager.NameNotFoundException {
        PackageInfo info = app.getPackageManager().getPackageInfo(app.getPackageName(), PackageManager.GET_PERMISSIONS);
        String[] permissions = info.requestedPermissions;//This array contains the requested permissions.

        Log.i("PERMISSIONS", "************PERMISSIONS LIST*************");

        ActivityCompat.requestPermissions(app, permissions, ALL_PERMISSIONS); /** gets all permissions from manifest! */

        // insert all permissions needed
        GoogleSignInOptionsExtension fitnessOptions =
                FitnessOptions.builder()
                        .addDataType(TYPE_STEP_COUNT_DELTA,FitnessOptions.ACCESS_READ)
                        .addDataType(TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
                        .addDataType(TYPE_CALORIES_EXPENDED,FitnessOptions.ACCESS_READ)
                        .addDataType(TYPE_ACTIVITY_SEGMENT,FitnessOptions.ACCESS_READ)
                        .build();

        if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(app), fitnessOptions)) {
            GoogleSignIn.requestPermissions( app,1, GoogleSignIn.getLastSignedInAccount(app),
                    fitnessOptions);
            Log.i("PERMISSIONS", "************PERMISSIONS REQUESTED*************");

        }
    }
}

the first screen I get is choosing google account: this screen pops up always then I suppose to get this screen but I almost never do: almost never pops up

and then the final screen of approving location pops up: permissions from phone always pops up

can't figure out why the 2nd screen doesn't show and I have to get those permission to manage the use of the data collected by the google-fit

Thanks.

Manspof
  • 598
  • 26
  • 81
  • 173
  • 3
    Is the log `PERMISSIONS REQUESTED` printed to logcat? And what result do you get in `onActivityResult`? Also one problem I noticed is that you call `ActivityCompat.requestPermissions` but doesn't wait for its result before requesting the GoogleSignIn permissions. The Android OS has some code that prevents "clickjacking" for permission requests, so that may prevent you from starting the second permission request (which I believe is a startActivity call) – Maurice Lam Jan 07 '21 at 08:14
  • 1
    Agree with @maurice here, the second step should be performed after the first permission dialog has been confirmed to be completed. The second dialog may as well just be dropped since a permission request has been made. I suggest requesting the Fit permission by having the user clicking a button. – JensV Jan 07 '21 at 19:51

1 Answers1

1

Put your logic in its own function(s), outside of onCreate().

private void youLogic() {
    // some code
}

If the permissions are granted, just call that function directly from onCreate():

@Override
public void onCreate(Bundle savedInstanceState) {
    //...
    if (checkSelfPermissions(/*whatever*/) {
        youLogic();
    } else {
        requestPermissions(/*whatever*/);
    }

    //nothing dependent on runtime permissions after this point
}

Then override onRequestPermissionsResult():

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == /*whatever you set in requestPermissions()*/) {
        if (!Arrays.asList(grantResults).contains(PackageManager.PERMISSION_DENIED)) {
            //all permissions have been granted
            youLogic(); //call your dependent logic
        }
    }
}

You may also check all permissions in manifest:

if(permissions.Contains(info)) {
  
  // request all other permissions here
  .
  .
  .
}