Java Code Examples for com.google.android.gms.auth.api.signin.GoogleSignIn#hasPermissions()

The following examples show how to use com.google.android.gms.auth.api.signin.GoogleSignIn#hasPermissions() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SignInActivityWithDrive.java    From google-services with Apache License 2.0 10 votes vote down vote up
@Override
public void onStart() {
    super.onStart();

    // Check if the user is already signed in and all required scopes are granted
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    if (account != null && GoogleSignIn.hasPermissions(account, new Scope(Scopes.DRIVE_APPFOLDER))) {
        updateUI(account);
    } else {
        updateUI(null);
    }
}
 
Example 2
Source File: RestApiActivity.java    From google-services with Apache License 2.0 9 votes vote down vote up
@Override
public void onStart() {
    super.onStart();

    // Check if the user is already signed in and all required scopes are granted
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    if (GoogleSignIn.hasPermissions(account, new Scope(CONTACTS_SCOPE))) {
        updateUI(account);
    } else {
        updateUI(null);
    }
}
 
Example 3
Source File: ConnectionController.java    From PGSGP with MIT License 8 votes vote down vote up
public Pair<Boolean, String> isConnected() {
    GoogleSignInAccount googleSignInAccount = GoogleSignIn.getLastSignedInAccount(activity);
    
    String accId = "";
    if (googleSignInAccount != null && googleSignInAccount.getId() != null) {
        accId = googleSignInAccount.getId();
    }
    return new Pair<>(GoogleSignIn.hasPermissions(googleSignInAccount, signInOptions.getScopeArray()), accId);
}
 
Example 4
Source File: UserFragment.java    From android-kubernetes-blockchain with Apache License 2.0 6 votes vote down vote up
@Override
public void onRefresh() {
    refreshRunnable = new Runnable() {
        @Override
        public void run() {
            getStateOfUser(userIdFromStorage);

            // refresh google fit history
            if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount((AppCompatActivity) getActivity()))) {
                Log.d(TAG, "Not signed in...");
            } else {
                accessGoogleFit();
            }
        }
    };

    handler.postDelayed(refreshRunnable,5000);
}
 
Example 5
Source File: LoadingActivity.java    From PretendYoureXyzzyAndroid with GNU General Public License v3.0 6 votes vote down vote up
private void signInSilently() {
    GoogleSignInOptions signInOptions = GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN;
    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
    if (GoogleSignIn.hasPermissions(account, signInOptions.getScopeArray())) {
        googleSignedIn(account);
    } else {
        GoogleSignInClient signInClient = GoogleSignIn.getClient(this, signInOptions);
        signInClient.silentSignIn().addOnCompleteListener(this, task -> {
            if (task.isSuccessful()) {
                googleSignedIn(task.getResult());
            } else {
                if (Prefs.getBoolean(PK.SHOULD_PROMPT_GOOGLE_PLAY, true)) {
                    Intent intent = signInClient.getSignInIntent();
                    startActivityForResult(intent, GOOGLE_SIGN_IN_CODE);
                }
            }
        });
    }
}
 
Example 6
Source File: Manager.java    From react-native-fitness with MIT License 5 votes vote down vote up
public boolean isAuthorized(final Activity activity, final ArrayList<Request> permissions){
    if(isGooglePlayServicesAvailable(activity)) {
        FitnessOptions fitnessOptions = addPermissionToFitnessOptions(FitnessOptions.builder(), permissions)
                .build();
        return GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount(activity), fitnessOptions);
    }
    return false;
}
 
Example 7
Source File: UserFragment.java    From android-kubernetes-blockchain with Apache License 2.0 5 votes vote down vote up
@Override
public void onPause() {
    super.onPause();

    // remove callbacks from refreshing
    handler.removeCallbacks(refreshRunnable);

    // set refreshing back to false if it was refreshing
    if (swipeRefreshLayout.isRefreshing()) {
        swipeRefreshLayout.setRefreshing(false);
    }

    // unregister the listener
    if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount((AppCompatActivity) getActivity()))) {
        Log.d(TAG, "Not signed in...");
    } else {
        Fitness.getSensorsClient((AppCompatActivity) getActivity(), GoogleSignIn.getLastSignedInAccount((AppCompatActivity) getActivity()))
                .remove(stepListener)
                .addOnCompleteListener(
                        new OnCompleteListener<Boolean>() {
                            @Override
                            public void onComplete(@NonNull Task<Boolean> task) {
                                if (task.isSuccessful() && task.getResult()) {
                                    Log.d(TAG, "Step Listener for steps was removed.");
                                } else {
                                    Log.e(TAG, "Step Listener for steps was not removed.", task.getException());
                                }
                            }
                        }
                );
    }
}
 
Example 8
Source File: GoogleSignInManager.java    From react-native-google-fitness with MIT License 5 votes vote down vote up
/**
 * Request GoogleFit API via GoogleSignIn
 *
 * @param activity
 * @param listener
 * @return false if already signed in
 */
public boolean requestPermissions(Activity activity, FitnessOptions fitnessOptions, ResultListener listener) {
    mListener = listener;

    GoogleSignInAccount lastSignedInAccount = GoogleSignIn.getLastSignedInAccount(mReactContext);
    if (!GoogleSignIn.hasPermissions(lastSignedInAccount, fitnessOptions)) {
        GoogleSignIn.requestPermissions(
                activity,
                REQUEST_OAUTH_REQUEST_CODE,
                lastSignedInAccount,
                fitnessOptions);
        return true;
    }
    return false;
}
 
Example 9
Source File: PlayGames.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
public void connect() {

		if(isConnected() || connecting) {
			return;
		}

		connecting = true;

		GoogleSignInOptions signInOptions =
				new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
						.requestScopes(Games.SCOPE_GAMES_SNAPSHOTS)
						.build();

		GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(Game.instance());
		if (GoogleSignIn.hasPermissions(account, signInOptions.getScopeArray())) {
			signedInAccount = account;
			onConnected();
		} else {
			GoogleSignIn.getClient(Game.instance(), signInOptions)
					.silentSignIn()
					.addOnCompleteListener(
							Game.instance().serviceExecutor,
							task -> {
								if (task.isSuccessful()) {
									 signedInAccount = task.getResult();
									 onConnected();
								} else {
                                    //Preferences.INSTANCE.put(Preferences.KEY_USE_PLAY_GAMES, false);
                                }
							});
		}
	}
 
Example 10
Source File: UserFragment.java    From android-kubernetes-blockchain with Apache License 2.0 4 votes vote down vote up
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_user, container, false);

//        ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
//        actionBar.show();
//        actionBar.setTitle("Footsteps");

        // attach labels
        userSteps = rootView.findViewById(R.id.numberOfSteps);
        distanceFromSteps = rootView.findViewById(R.id.distance);
        userId = rootView.findViewById(R.id.userIdText);
        coinsBalance = rootView.findViewById(R.id.numberOfCoins);
        swipeRefreshLayout = rootView.findViewById(R.id.swipeRefresh);
        swipeRefreshLayout.setOnRefreshListener(this);

        // request queue
        queue = Volley.newRequestQueue((AppCompatActivity) getActivity());

        // initialize shared preferences - persistent data
        SharedPreferences sharedPreferences = ((AppCompatActivity) getActivity()).getSharedPreferences("shared_preferences_fitcoin", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();

        // check if enrolled in blockchain network
        if (sharedPreferences.contains("BlockchainUserId")) {
            userIdFromStorage = sharedPreferences.getString("BlockchainUserId","Something went wrong...");
            userId.setText(userIdFromStorage);
            if (!userIdFromStorage.equals("Something went wrong...")) {
                getStateOfUser(userIdFromStorage);
            }
        } else {
            userId.setText(R.string.notEnrolled);
        }

        // Get the date now
        Calendar cal = Calendar.getInstance();
        cal.setTime(new Date());

        // if DateStarted is existing, use it
        if (sharedPreferences.contains("DateStarted")) {
            userStartingDate = sharedPreferences.getLong("DateStarted", cal.getTimeInMillis());
        } else { // else use time and date now and persist it for later use
            userStartingDate = cal.getTimeInMillis();
            editor.putLong("DateStarted",userStartingDate);
            editor.apply();
        }

        // build the fitness options
        FitnessOptions fitnessOptions = FitnessOptions.builder()
                .addDataType(DataType.TYPE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                .addDataType(DataType.AGGREGATE_STEP_COUNT_DELTA, FitnessOptions.ACCESS_READ)
                .addDataType(DataType.TYPE_DISTANCE_DELTA, FitnessOptions.ACCESS_READ)
                .build();

        // check if app has permissions
        if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount((AppCompatActivity) getActivity()), fitnessOptions)) {
            GoogleSignIn.requestPermissions(
                    this,
                    REQUEST_OAUTH_REQUEST_CODE,
                    GoogleSignIn.getLastSignedInAccount((AppCompatActivity) getActivity()),
                    fitnessOptions);
        } else {
            accessGoogleFit();
        }

        return rootView;
    }
 
Example 11
Source File: UserFragment.java    From android-kubernetes-blockchain with Apache License 2.0 4 votes vote down vote up
@Override
public void onResume() {
    super.onResume();
    Log.d(TAG, "Resumed app");

    // initialize the step listener
    stepListener = new OnDataPointListener() {
        @Override
        public void onDataPoint(DataPoint dataPoint) {
            for (Field field : dataPoint.getDataType().getFields()) {
                Log.d(TAG, "Field: " + field.getName());
                Log.d(TAG, "Value: " + dataPoint.getValue(field));

                if (field.getName().equals("steps")) {
                    int currentSteps = Integer.valueOf(userSteps.getText().toString());
                    currentSteps = currentSteps + dataPoint.getValue(field).asInt();
                    userSteps.setText(Integer.toString(currentSteps));

                    // if nothing in sending in totalStepsConvertedToFitcoin
                    if (totalStepsConvertedToFitcoin != null && !sendingInProgress) {
                        if (currentSteps - totalStepsConvertedToFitcoin > FITCOINS_STEPS_CONVERSION) {
                            sendingInProgress = true;

                            // send steps to blockchain
                            sendStepsToFitchain(userIdFromStorage,currentSteps);

                            // insert send steps to mongo

                            // insert logic for leaderboards
                        }
                    }
                }
            }
        }
    };

    // register the step listener
    if (!GoogleSignIn.hasPermissions(GoogleSignIn.getLastSignedInAccount((AppCompatActivity) getActivity()))) {
        Log.d(TAG, "Not signed in...");
    } else {
        Fitness.getSensorsClient((AppCompatActivity) getActivity(), GoogleSignIn.getLastSignedInAccount((AppCompatActivity) getActivity()))
                .add(
                        new SensorRequest.Builder()
                                .setDataType(DataType.TYPE_STEP_COUNT_DELTA)
                                .setSamplingRate(10, TimeUnit.SECONDS)
                                .build(), stepListener
                )
                .addOnCompleteListener(
                        new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Log.d(TAG, "Step Listener Registered.");
                                } else {
                                    Log.e(TAG, "Step Listener not registered", task.getException());
                                }
                            }
                        }
                );
    }
}