Muhammad Ahmed
Muhammad Ahmed

Reputation: 113

Cannot resolve GOOGLE_FIT_PERMISSIONS_REQUEST_CODE

I am using Google Fit API in Android Studio and I was following the this tutorial, but an error occurred, saying:

cannot resolve the symbol.

I have searched for a solution for this, but I could not find one.

I have also put the dependencies on my module gradle, as told in their website.

GOOGLE_FIT_PERMISSIONS_REQUEST_CODE

Here is the code I have copied from the above mentioned link:

  public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FitnessOptions fitnessOptions = FitnessOptions.builder()
                .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                .build();
        if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(this), fitnessOptions)) {
            GoogleSignIn.requestPermissions(this,  GOOGLE_FIT_PERMISSIONS_REQUEST_CODE, GoogleSignIn.getLastSignedInAccount(this), fitnessOptions);
        } else {

            accessGoogleFit();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode ==  GOOGLE_FIT_PERMISSIONS_REQUEST_CODE) {
                accessGoogleFit();
            }
        }
    }
    private void accessGoogleFit() {
        final  String LOG_TAG="i am here";
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());
        long endTime = cal.getTimeInMillis();
        cal.add(Calendar.YEAR, -1);
        long startTime = cal.getTimeInMillis();

        DataReadRequest readRequest = new DataReadRequest.Builder()
                .aggregate(DataType.TYPE_STEP_COUNT_DELTA, DataType.AGGREGATE_STEP_COUNT_DELTA)
                .setTimeRange(startTime, endTime, TimeUnit.MILLISECONDS)
                .build();


        Fitness.getHistoryClient(this, GoogleSignIn.getLastSignedInAccount(this))
                .readData(readRequest)
                .addOnSuccessListener(new OnSuccessListener() {
                    @Override
                    public void onSuccess(Object o) {
                        Log.d(LOG_TAG, "onSuccess()");
                    }


                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Log.e(LOG_TAG, "onFailure()", e);
                    }
                })
                .addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        Log.d(LOG_TAG, "onComplete()");
                    }
                });
    }
}

Upvotes: 2

Views: 1843

Answers (2)

gb96
gb96

Reputation: 1694

The requestCode is part of a Service Callback design pattern. The way the example is written implies the possibility that your app code somehow receives a callback for a service request that was made by a different client instance.

The test for a matching requestCode parameter passed in onActivityResult() is a defensive coding guard against an instance of your app code responding to the result of a service request that it did not initiate. Since we are talking about app permissions here, this is a privacy and security concern. This hypothetical "rouge callback" might be generated by malware on the user device, or a compromised service end-point, or some kind of man-in-the-middle (MITM) attacker on the network.

Unfortunately hard-coding a fixed requestCode as is very often done might not provide the best protection against this scenario, since every instance of your class uses the same value.

For a more dynamic value of requestCode you could use something like this instead (noting request code is only allowed to use the lowest 16 bits of the integer, so we mask it using the bitwise '&' operator):

final int GOOGLE_FIT_PERMISSIONS_REQUEST_CODE = System.identityHashCode(this) & 0xFFFF;

Upvotes: 4

Bruno
Bruno

Reputation: 4007

You haven't define it.

public class MainActivity extends AppCompatActivity {
  int GOOGLE_FIT_PERMISSIONS_REQUEST_CODE = 123456; //whatever you want
  // rest of the code
}

Upvotes: 5

Related Questions