Esempio n. 1
0
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            if (requestCode == REQUEST_GOOGLE_FITNESS_AUTH)
            {
                authInProgress = false;
                HasGoogleFitnessAuthentication = resultCode == Result.Ok;
                System.Diagnostics.Debug.WriteLine($"Result after auth is: {HasGoogleFitnessAuthentication}");
                GoogleFitnessAuthenticationUpdated?.Invoke(HasGoogleFitnessAuthentication);
            }

            if (requestCode == REQUEST_GOOGLE_SIGN_IN)
            {
                googleLoginInProgress = false;
                var signInTask = GoogleSignIn.GetSignedInAccountFromIntent(data);
                if (signInTask.IsSuccessful)
                {
                    account = (GoogleSignInAccount)signInTask.Result;
                    RequestFitnessPermissions();
                }
            }

            if (requestCode == REQUEST_FITNESS_PERMISSIONS)
            {
                HasGoogleFitnessPermissions = resultCode == Result.Ok;
                GoogleFitnessPermissionsUpdated?.Invoke(HasGoogleFitnessPermissions);
            }
        }
Esempio n. 2
0
        public void RequestFitnessPermissions()
        {
            //we have to do all this hullaballoo because Xamarin.PlayServices.Fitness does not contain a constructor for FitnessOptions
            //IntPtr classRef = JNIEnv.FindClass("com/google/android/gms/fitness/FitnessOptions$Builder");
            //IntPtr constructorId = JNIEnv.GetMethodID(classRef, "<init>", "I(V)");
            //IntPtr referenceInstance = JNIEnv.NewObject(classRef, constructorId);

            //var fitnessApiOptions = new FitnessOptions.Builder()
            var fitnessApiOptions = FitnessOptions.InvokeBuilder()
                                    .AddDataType(Android.Gms.Fitness.Data.DataType.AggregateActivitySummary, FitnessOptions.AccessWrite)
                                    .AddDataType(Android.Gms.Fitness.Data.DataType.AggregateSpeedSummary, FitnessOptions.AccessWrite)
                                    .Build();

            account = GoogleSignIn.GetLastSignedInAccount(this);

            if (account == null && !googleLoginInProgress)
            {
                GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DefaultSignIn)
                                          .RequestEmail()
                                          .Build();

                var signInClient = GoogleSignIn.GetClient(this, gso);
                googleLoginInProgress = true;
                StartActivityForResult(signInClient.SignInIntent, REQUEST_GOOGLE_SIGN_IN);
                return;
            }

            if (!GoogleSignIn.HasPermissions(account, fitnessApiOptions))
            {
                GoogleSignIn.RequestPermissions(
                    this,
                    REQUEST_FITNESS_PERMISSIONS,
                    account,
                    fitnessApiOptions);
            }
            else
            {
                HasGoogleFitnessPermissions = true;
                GoogleFitnessPermissionsUpdated?.Invoke(HasGoogleFitnessPermissions);
            }
        }