Wednesday 6 January 2021

sync permissions with an android app Google-Fit

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.



from sync permissions with an android app Google-Fit

No comments:

Post a Comment