Search in sources :

Example 1 with WakeUpReceiver

use of forpdateam.ru.forpda.common.receivers.WakeUpReceiver in project ForPDA by RadiationX.

the class App method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
    long time = System.currentTimeMillis();
    ACRA.init(this);
    ACRA.getErrorReporter().putCustomData("USER_NICK", getPreferences().getString("auth.user.nick", "null"));
    RxJavaPlugins.setErrorHandler(throwable -> {
        Log.d("SUKA", "RxJavaPlugins errorHandler " + throwable);
        throwable.printStackTrace();
    });
    setTheme(isDarkTheme() ? R.style.DarkAppTheme : R.style.LightAppTheme);
    try {
        String inputHistory = getPreferences().getString("app.versions.history", "");
        String[] history = TextUtils.split(inputHistory, ";");
        int lastVNum = 0;
        boolean disorder = false;
        for (String version : history) {
            int vNum = Integer.parseInt(version);
            if (vNum < lastVNum) {
                disorder = true;
            }
            lastVNum = vNum;
        }
        Object vCode = BuildConfig.VERSION_CODE;
        String sVCode = "" + vCode;
        int nVCode = Integer.parseInt(sVCode);
        if (lastVNum < nVCode) {
            List<String> list = new ArrayList<>(Arrays.asList(history));
            list.add(Integer.toString(nVCode));
            getPreferences().edit().putString("app.versions.history", TextUtils.join(";", list)).apply();
        }
        if (disorder) {
            throw new Exception("Нарушение порядка версий!");
        }
    } catch (Exception ex) {
        ex.printStackTrace();
        ACRA.getErrorReporter().putCustomData("VERSIONS_HISTORY", getPreferences().getString("app.versions.history", ""));
        ACRA.getErrorReporter().handleException(ex);
    }
    ACRA.getErrorReporter().putCustomData("VERSIONS_HISTORY", getPreferences().getString("app.versions.history", ""));
    density = getResources().getDisplayMetrics().density;
    initImageLoader(this);
    px2 = getContext().getResources().getDimensionPixelSize(R.dimen.dp2);
    px4 = getContext().getResources().getDimensionPixelSize(R.dimen.dp4);
    px6 = getContext().getResources().getDimensionPixelSize(R.dimen.dp6);
    px8 = getContext().getResources().getDimensionPixelSize(R.dimen.dp8);
    px12 = getContext().getResources().getDimensionPixelSize(R.dimen.dp12);
    px14 = getContext().getResources().getDimensionPixelSize(R.dimen.dp14);
    px16 = getContext().getResources().getDimensionPixelSize(R.dimen.dp16);
    px20 = getContext().getResources().getDimensionPixelSize(R.dimen.dp20);
    px24 = getContext().getResources().getDimensionPixelSize(R.dimen.dp24);
    px32 = getContext().getResources().getDimensionPixelSize(R.dimen.dp32);
    px36 = getContext().getResources().getDimensionPixelSize(R.dimen.dp36);
    px40 = getContext().getResources().getDimensionPixelSize(R.dimen.dp40);
    px48 = getContext().getResources().getDimensionPixelSize(R.dimen.dp48);
    px56 = getContext().getResources().getDimensionPixelSize(R.dimen.dp56);
    px64 = getContext().getResources().getDimensionPixelSize(R.dimen.dp64);
    for (Field f : R.string.class.getFields()) {
        try {
            if (f.getName().contains("res_s_")) {
                templateStringCache.put(f.getName(), getString(f.getInt(f)));
            }
        } catch (Exception ignore) {
        }
    }
    keyboardHeight = getPreferences().getInt("keyboard_height", getContext().getResources().getDimensionPixelSize(R.dimen.default_keyboard_height));
    savedKeyboardHeight = keyboardHeight;
    Realm.init(this);
    RealmConfiguration configuration = new RealmConfiguration.Builder().name("forpda.realm").schemaVersion(4).migration(new DbMigration()).build();
    Realm.setDefaultConfiguration(configuration);
    Client.get(getContext());
    getPreferences().registerOnSharedPreferenceChangeListener(preferenceChangeListener);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        BroadcastReceiver receiver = new BroadcastReceiver() {

            @RequiresApi(api = Build.VERSION_CODES.M)
            @Override
            public void onReceive(Context context, Intent intent) {
                Log.d(App.class.getSimpleName(), "DOZE ON RECEIVE " + intent);
                PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
                if (pm == null)
                    return;
                if (pm.isDeviceIdleMode()) {
                    // the device is now in doze mode
                    Log.d(App.class.getSimpleName(), "DOZE MODE ENABLYA");
                } else {
                    // the device just woke up from doze mode
                    Log.d(App.class.getSimpleName(), "DOZE MODE DISABLYA");
                    NotificationsService.startAndCheck();
                }
            }
        };
        registerReceiver(receiver, new IntentFilter(PowerManager.ACTION_DEVICE_IDLE_MODE_CHANGED));
    }
    IntentFilter wakeUpFilter = new IntentFilter();
    wakeUpFilter.addAction(Intent.ACTION_BOOT_COMPLETED);
    wakeUpFilter.addAction(Intent.ACTION_SCREEN_ON);
    registerReceiver(new WakeUpReceiver(), wakeUpFilter);
    // На каких-то диких калькуляторах может быть ANR, поэтому в фоновый поток
    Observable.fromCallable(() -> {
        JobConfig.addLogger((priority, tag, message, t) -> {
            Log.e("JobLogger", "Job: pr=" + priority + "; t=" + tag + "; m=" + message + "; th=" + t);
        });
        JobConfig.setLogcatEnabled(false);
        JobManager.create(this).addJobCreator(new NotificationsJobCreator());
        JobManager.instance().cancelAllForTag(NotificationsJob.TAG);
        new JobRequest.Builder(NotificationsJob.TAG).setPeriodic(TimeUnit.MINUTES.toMillis(16L)).setRequiresCharging(false).setRequiresDeviceIdle(false).setRequiredNetworkType(JobRequest.NetworkType.ANY).build().schedule();
        return true;
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe();
    QmsHelper.init();
    Log.e("APP", "TIME APP FINAL " + (System.currentTimeMillis() - time));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Observer(java.util.Observer) Arrays(java.util.Arrays) PackageManager(android.content.pm.PackageManager) TabNotification(forpdateam.ru.forpda.data.models.TabNotification) Drawable(android.graphics.drawable.Drawable) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) IBinder(android.os.IBinder) Preferences(forpdateam.ru.forpda.common.Preferences) Manifest(android.Manifest) PowerManager(android.os.PowerManager) ACRA(org.acra.ACRA) ByteArrayInputStream(java.io.ByteArrayInputStream) Handler(android.os.Handler) Map(java.util.Map) ServiceConnection(android.content.ServiceConnection) AttrRes(android.support.annotation.AttrRes) TabFragment(forpdateam.ru.forpda.ui.fragments.TabFragment) Schedulers(io.reactivex.schedulers.Schedulers) PreferenceManager(android.preference.PreferenceManager) Log(android.util.Log) JobConfig(com.evernote.android.job.JobConfig) Realm(io.realm.Realm) IntentFilter(android.content.IntentFilter) ImageLoaderConfiguration(com.nostra13.universalimageloader.core.ImageLoaderConfiguration) RxJavaPlugins(io.reactivex.plugins.RxJavaPlugins) BroadcastReceiver(android.content.BroadcastReceiver) ActivityCompat(android.support.v4.app.ActivityCompat) APP_VERSION_NAME(org.acra.ReportField.APP_VERSION_NAME) DisplayMetrics(android.util.DisplayMetrics) ANDROID_VERSION(org.acra.ReportField.ANDROID_VERSION) List(java.util.List) NotificationsJobCreator(forpdateam.ru.forpda.notifications.NotificationsJobCreator) RealmConfiguration(io.realm.RealmConfiguration) UsingFreqLimitedMemoryCache(com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache) Pattern(java.util.regex.Pattern) USER_APP_START_DATE(org.acra.ReportField.USER_APP_START_DATE) STACK_TRACE(org.acra.ReportField.STACK_TRACE) MiniTemplator(biz.source_code.miniTemplator.MiniTemplator) Context(android.content.Context) JobRequest(com.evernote.android.job.JobRequest) VectorDrawableCompat(android.support.graphics.drawable.VectorDrawableCompat) ReportsCrashes(org.acra.annotation.ReportsCrashes) OnSharedPreferenceChangeListener(android.content.SharedPreferences.OnSharedPreferenceChangeListener) HashCodeFileNameGenerator(com.nostra13.universalimageloader.cache.disc.naming.HashCodeFileNameGenerator) Intent(android.content.Intent) RequiresApi(android.support.annotation.RequiresApi) HashMap(java.util.HashMap) NonNull(android.support.annotation.NonNull) APP_VERSION_CODE(org.acra.ReportField.APP_VERSION_CODE) CustomCrashDialog(forpdateam.ru.forpda.ui.activities.CustomCrashDialog) TypedArray(android.content.res.TypedArray) ArrayList(java.util.ArrayList) LocaleHelper(forpdateam.ru.forpda.common.LocaleHelper) Cookie(okhttp3.Cookie) WebSettings(android.webkit.WebSettings) Charset(java.nio.charset.Charset) QmsHelper(forpdateam.ru.forpda.ui.fragments.qms.QmsHelper) CUSTOM_DATA(org.acra.ReportField.CUSTOM_DATA) VectorDrawable(android.graphics.drawable.VectorDrawable) Toast(android.widget.Toast) Client(forpdateam.ru.forpda.client.Client) Observable(io.reactivex.Observable) Build(android.os.Build) DbMigration(forpdateam.ru.forpda.common.realm.DbMigration) NotificationsJob(forpdateam.ru.forpda.notifications.NotificationsJob) WakeUpReceiver(forpdateam.ru.forpda.common.receivers.WakeUpReceiver) ComponentName(android.content.ComponentName) FadeInBitmapDisplayer(com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer) TextUtils(android.text.TextUtils) BaseImageDownloader(com.nostra13.universalimageloader.core.download.BaseImageDownloader) IOException(java.io.IOException) Messenger(android.os.Messenger) AppCompatResources(android.support.v7.content.res.AppCompatResources) ImageLoader(com.nostra13.universalimageloader.core.ImageLoader) Field(java.lang.reflect.Field) LOGCAT(org.acra.ReportField.LOGCAT) DisplayImageOptions(com.nostra13.universalimageloader.core.DisplayImageOptions) PHONE_MODEL(org.acra.ReportField.PHONE_MODEL) Color(android.graphics.Color) ColorInt(android.support.annotation.ColorInt) TimeUnit(java.util.concurrent.TimeUnit) ReportingInteractionMode(org.acra.ReportingInteractionMode) DrawableRes(android.support.annotation.DrawableRes) SharedPreferences(android.content.SharedPreferences) TypedValue(android.util.TypedValue) Bitmap(android.graphics.Bitmap) NotificationsService(forpdateam.ru.forpda.notifications.NotificationsService) JobManager(com.evernote.android.job.JobManager) Activity(android.app.Activity) InputStream(java.io.InputStream) SimpleObservable(forpdateam.ru.forpda.common.simple.SimpleObservable) ArrayList(java.util.ArrayList) NotificationsJobCreator(forpdateam.ru.forpda.notifications.NotificationsJobCreator) PowerManager(android.os.PowerManager) Field(java.lang.reflect.Field) RealmConfiguration(io.realm.RealmConfiguration) JobRequest(com.evernote.android.job.JobRequest) DbMigration(forpdateam.ru.forpda.common.realm.DbMigration) Context(android.content.Context) IntentFilter(android.content.IntentFilter) Intent(android.content.Intent) BroadcastReceiver(android.content.BroadcastReceiver) IOException(java.io.IOException) WakeUpReceiver(forpdateam.ru.forpda.common.receivers.WakeUpReceiver)

Aggregations

Manifest (android.Manifest)1 Activity (android.app.Activity)1 BroadcastReceiver (android.content.BroadcastReceiver)1 ComponentName (android.content.ComponentName)1 Context (android.content.Context)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 ServiceConnection (android.content.ServiceConnection)1 SharedPreferences (android.content.SharedPreferences)1 OnSharedPreferenceChangeListener (android.content.SharedPreferences.OnSharedPreferenceChangeListener)1 PackageManager (android.content.pm.PackageManager)1 TypedArray (android.content.res.TypedArray)1 Bitmap (android.graphics.Bitmap)1 Color (android.graphics.Color)1 Drawable (android.graphics.drawable.Drawable)1 VectorDrawable (android.graphics.drawable.VectorDrawable)1 Build (android.os.Build)1 Handler (android.os.Handler)1 IBinder (android.os.IBinder)1 Messenger (android.os.Messenger)1