Advertisement
afarber

Custom MultiDexApplication class

Apr 1st, 2022
933
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.60 KB | None | 0 0
  1. package de.afarber;
  2.  
  3. import static de.afarber.Utils.HUAWEI;
  4. import static de.afarber.Utils.TAG;
  5. import static de.afarber.Utils.createJsonLogin;
  6.  
  7. import android.app.Activity;
  8. import android.app.PendingIntent;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.content.SharedPreferences;
  12. import android.preference.PreferenceManager;
  13. import android.text.TextUtils;
  14. import android.util.Log;
  15. import android.widget.Toast;
  16.  
  17. import androidx.annotation.NonNull;
  18.  
  19. import com.huawei.hmf.tasks.Task;
  20. import com.huawei.hms.aaid.HmsInstanceId;
  21. import com.huawei.hms.common.ResolvableApiException;
  22. import com.huawei.hms.push.HmsMessaging;
  23. import com.huawei.hms.support.account.AccountAuthManager;
  24. import com.huawei.hms.support.account.request.AccountAuthParams;
  25. import com.huawei.hms.support.account.request.AccountAuthParamsHelper;
  26. import com.huawei.hms.support.account.result.AuthAccount;
  27. import com.huawei.hms.support.account.service.AccountAuthService;
  28.  
  29. import org.json.JSONObject;
  30.  
  31. import java.util.concurrent.Executors;
  32. import java.util.concurrent.ScheduledExecutorService;
  33.  
  34. public class SlovaApplication extends BaseApplication {
  35.     private static final int HMS_LOGIN = 1979;
  36.  
  37.     private final ScheduledExecutorService mExecutor = Executors.newSingleThreadScheduledExecutor();
  38.  
  39.     @Override
  40.     public void onCreate(@NonNull Context context) {
  41.         // generate AAID before requesting push token
  42.         HmsMessaging.getInstance(this).setAutoInitEnabled(true);
  43.  
  44.         mExecutor.execute(() -> {
  45.             try {
  46.                 // request push token for this Huawei device
  47.                 String appId = context.getString(R.string.huawei_app_id);
  48.                 String token = HmsInstanceId.getInstance(context).getToken(appId, "HCM");
  49.                 if (!TextUtils.isEmpty(token)) {
  50.                     // this only works for EMUI 10 or newer devices, otherwise HmsService is called
  51.                     Log.d(Utils.TAG,"getToken token=" + token);
  52.                     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
  53.                     prefs.edit().putString(KEY_TOKEN, token).apply();
  54.                 }
  55.             } catch (ResolvableApiException ex) {
  56.                 Log.w(TAG,"getToken failed", ex);
  57.                 tryToResolve(context, ex);
  58.             } catch (Exception ex) {
  59.                 Log.w(TAG,"getToken failed", ex);
  60.             }
  61.         });
  62.     }
  63.  
  64.     @Override
  65.     public boolean onActivityResult(@NonNull Context context, int requestCode, int resultCode, Intent data) {
  66.         Log.d(TAG, "onActivityResult requestCode=" + requestCode +
  67.                 ", resultCode=" + resultCode +
  68.                 ", data=" + (data == null ? "null" : data.toUri(0)));
  69.  
  70.         if (requestCode == HMS_LOGIN) {
  71.             Task<AuthAccount> task = AccountAuthManager.parseAuthResultFromIntent(data);
  72.             saveJsonLogin(context, task);
  73.             return true;
  74.         }
  75.  
  76.         return false;
  77.     }
  78.  
  79.     private void saveJsonLogin(@NonNull Context context, Task<AuthAccount> task) {
  80.         if (task != null && task.isSuccessful()) {
  81.             AuthAccount account = task.getResult();
  82.             if (account != null) {
  83.                 try {
  84.                     JSONObject jsonLogin = createJsonLogin(
  85.                             HUAWEI,
  86.                             // if open id (same with web) is missing, fall back to open id (same across all apps)
  87.                             TextUtils.isEmpty(account.getOpenId()) ? account.getUnionId() : account.getOpenId(),
  88.                             // if given name is missing, fall back to display name
  89.                             TextUtils.isEmpty(account.getGivenName()) ? account.getDisplayName() : account.getGivenName(),
  90.                             account.getFamilyName(),
  91.                             account.getAvatarUriString());
  92.                     SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
  93.                     prefs.edit().putString(KEY_JSON_LOGIN, jsonLogin.toString()).apply();
  94.                     Log.d(TAG, "saveJsonLogin jsonLogin=" + jsonLogin);
  95.                     return;
  96.                 } catch (Exception ex) {
  97.                     Log.w(TAG, "saveJsonLogin failed", ex);
  98.                 }
  99.             }
  100.         }
  101.  
  102.         if (task != null) {
  103.             Log.w(TAG, "saveJsonLogin failed", task.getException());
  104.         }
  105.  
  106.         Toast.makeText(context, getString(R.string.error_huawei_login), Toast.LENGTH_LONG).show();
  107.     }
  108.  
  109.     @Override
  110.     public void signin(@NonNull Activity activity) {
  111.         AccountAuthParams authParams = new AccountAuthParamsHelper(AccountAuthParams.DEFAULT_AUTH_REQUEST_PARAM).createParams();
  112.         AccountAuthService service = AccountAuthManager.getService(activity, authParams);
  113.         activity.startActivityForResult(service.getSignInIntent(), HMS_LOGIN);
  114.     }
  115.  
  116.     private static void tryToResolve(@NonNull Context context, @NonNull ResolvableApiException ex) {
  117.         PendingIntent resolution = ex.getResolution();
  118.         Log.d(TAG,"tryToResolve resolution=" + resolution);
  119.         if (resolution != null) {
  120.             try {
  121.                 resolution.send();
  122.             } catch (PendingIntent.CanceledException ex2) {
  123.                 Log.w(TAG,"tryToResolve failed", ex2);
  124.             }
  125.         } else {
  126.             Intent resolutionIntent = ex.getResolutionIntent();
  127.             if (resolutionIntent != null) {
  128.                 resolutionIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  129.                 context.startActivity(resolutionIntent);
  130.             }
  131.         }
  132.     }
  133. }
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement