ECBaseTask.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package epson.epsonconnectregistration;
  2. import android.app.Activity;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.content.res.Configuration;
  6. import android.net.Uri;
  7. import android.os.AsyncTask;
  8. import android.os.Build;
  9. import android.os.LocaleList;
  10. import java.lang.ref.WeakReference;
  11. public abstract class ECBaseTask extends AsyncTask<Void, Void, ECStatus> {
  12. static final String BASE_URI_CONFIG_SERVICE = "Epson_WS_Config";
  13. static final String CONTENT_TYPE_SOAP = "application/soap+xml";
  14. private static final String TAG = "ECBaseTask";
  15. WeakReference<Activity> activityWeakReference = null;
  16. Uri rootUri = null;
  17. public void setContext(Activity activity) {
  18. activityWeakReference = new WeakReference<>(activity);
  19. }
  20. public void setRootUri(Uri uri) {
  21. rootUri = uri;
  22. }
  23. private Context checkParam() {
  24. Context context = (Context) activityWeakReference.get();
  25. if (context == null || rootUri == null) {
  26. return null;
  27. }
  28. return context;
  29. }
  30. private Uri getEndpoint() {
  31. return Uri.withAppendedPath(this.rootUri, BASE_URI_CONFIG_SERVICE);
  32. }
  33. private void openWebPage(Activity activity, Uri uri) {
  34. EpLog.d(TAG, "Open WebConfig uri = " + uri.toString());
  35. activity.startActivity(new Intent("android.intent.action.VIEW", uri));
  36. }
  37. private String getLang(Context context) {
  38. String str = "en-US";
  39. Configuration configuration = context.getResources().getConfiguration();
  40. if (configuration != null) {
  41. if (Build.VERSION.SDK_INT >= 24) {
  42. LocaleList locales = configuration.getLocales();
  43. if (locales.size() > 0) {
  44. str = locales.get(0).toLanguageTag();
  45. }
  46. } else if (Build.VERSION.SDK_INT >= 21) {
  47. str = configuration.locale.toLanguageTag();
  48. } else {
  49. str = String.format("%s-%s", new Object[]{configuration.locale.getLanguage(), configuration.locale.getCountry()});
  50. }
  51. }
  52. if (!(str == null || str.length() == 5)) {
  53. String[] split = str.split("-");
  54. if (split.length > 2) {
  55. str = split[0] + "-" + split[2];
  56. }
  57. if (str.length() != 5) {
  58. str = String.format("%s-%s", new Object[]{configuration.locale.getLanguage(), configuration.locale.getCountry()});
  59. }
  60. }
  61. if (str == null || str.length() != 5) {
  62. str = "en-US";
  63. }
  64. EpLog.d(TAG, "getLang: " + str);
  65. return str;
  66. }
  67. }