Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.afarber;
- import static de.afarber.Utils.HUAWEI;
- import static de.afarber.Utils.TAG;
- import static de.afarber.Utils.createJsonLogin;
- import android.app.Activity;
- import android.app.PendingIntent;
- import android.content.Context;
- import android.content.Intent;
- import android.content.SharedPreferences;
- import android.preference.PreferenceManager;
- import android.text.TextUtils;
- import android.util.Log;
- import android.widget.Toast;
- import androidx.annotation.NonNull;
- import com.huawei.hmf.tasks.Task;
- import com.huawei.hms.aaid.HmsInstanceId;
- import com.huawei.hms.common.ResolvableApiException;
- import com.huawei.hms.push.HmsMessaging;
- import com.huawei.hms.support.account.AccountAuthManager;
- import com.huawei.hms.support.account.request.AccountAuthParams;
- import com.huawei.hms.support.account.request.AccountAuthParamsHelper;
- import com.huawei.hms.support.account.result.AuthAccount;
- import com.huawei.hms.support.account.service.AccountAuthService;
- import org.json.JSONObject;
- import java.util.concurrent.Executors;
- import java.util.concurrent.ScheduledExecutorService;
- public class SlovaApplication extends BaseApplication {
- private static final int HMS_LOGIN = 1979;
- private final ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor();
- @Override
- public void onCreate(@NonNull Context context) {
- // generate AAID before requesting push token
- HmsMessaging.getInstance(this).setAutoInitEnabled(true);
- mExecutor.execute(() -> {
- try {
- // request push token for this Huawei device
- String appId = context.getString(R.string.huawei_app_id);
- String token = HmsInstanceId.getInstance(context).getToken(appId, "HCM");
- if (!TextUtils.isEmpty(token)) {
- // this only works for EMUI 10 or newer devices, otherwise HmsService is called
- Log.d(Utils.TAG,"getToken token=" + token);
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
- prefs.edit().putString(KEY_TOKEN, token).apply();
- }
- } catch (ResolvableApiException ex) {
- Log.w(TAG,"getToken failed", ex);
- tryToResolve(context, ex);
- } catch (Exception ex) {
- Log.w(TAG,"getToken failed", ex);
- }
- });
- }
- @Override
- public boolean onActivityResult(@NonNull Context context, int requestCode, int resultCode, Intent data) {
- Log.d(TAG, "onActivityResult requestCode=" + requestCode +
- ", resultCode=" + resultCode +
- ", data=" + (data == null ? "null" : data.toUri(0)));
- if (requestCode == HMS_LOGIN) {
- Task<AuthAccount> task = AccountAuthManager.parseAuthResultFromIntent(data);
- saveJsonLogin(context, task);
- return true;
- }
- return false;
- }
- private void saveJsonLogin(@NonNull Context context, Task<AuthAccount> task) {
- if (task != null && task.isSuccessful()) {
- AuthAccount account = task.getResult();
- if (account != null) {
- try {
- JSONObject jsonLogin = createJsonLogin(
- HUAWEI,
- // if open id (same with web) is missing, fall back to open id (same across all apps)
- TextUtils.isEmpty(account.getOpenId()) ? account.getUnionId() : account.getOpenId(),
- // if given name is missing, fall back to display name
- TextUtils.isEmpty(account.getGivenName()) ? account.getDisplayName() : account.getGivenName(),
- account.getFamilyName(),
- account.getAvatarUriString());
- SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
- prefs.edit().putString(KEY_JSON_LOGIN, jsonLogin.toString()).apply();
- Log.d(TAG, "saveJsonLogin jsonLogin=" + jsonLogin);
- return;
- } catch (Exception ex) {
- Log.w(TAG, "saveJsonLogin failed", ex);
- }
- }
- }
- if (task != null) {
- Log.w(TAG, "saveJsonLogin failed", task.getException());
- }
- Toast.makeText(context, getString(R.string.error_huawei_login), Toast.LENGTH_LONG).show();
- }
- @Override
- public void signin(@NonNull Activity activity) {
- AccountAuthParams authParams = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).createParams();
- AccountAuthService service = AccountAuthManager.getService(activity, authParams);
- activity.startActivityForResult(service.getSignInIntent(), HMS_LOGIN);
- }
- private static void tryToResolve(@NonNull Context context, @NonNull ResolvableApiException ex) {
- PendingIntent resolution = ex.getResolution();
- Log.d(TAG,"tryToResolve resolution=" + resolution);
- if (resolution != null) {
- try {
- resolution.send();
- } catch (PendingIntent.CanceledException ex2) {
- Log.w(TAG,"tryToResolve failed", ex2);
- }
- } else {
- Intent resolutionIntent = ex.getResolutionIntent();
- if (resolutionIntent != null) {
- resolutionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- context.startActivity(resolutionIntent);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement