package com.idogfooding.backbone;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.support.multidex.MultiDex;

import com.bumptech.glide.request.target.ViewTarget;
import com.facebook.stetho.Stetho;

import java.util.List;

/**
 * Base Application
 *
 * @author Charles
 */
public class BaseApplication extends Application {

    static Context _context;
    static Resources _resource;

    @Override
    public void onCreate() {
        super.onCreate();
        _context = getApplicationContext();
        _resource = _context.getResources();

        // fix glide issue "You must not call setTag() on a view Glide is targeting"
        // https://github.com/bumptech/glide/issues/370
        // http://stackoverflow.com/questions/34833627/error-you-must-not-call-settag-on-a-view-glide-is-targeting-when-use-glide/35096552
        ViewTarget.setTagId(R.id.glide_tag);

        // init bugly
        // CrashReport.initCrashReport(getApplicationContext());

        // init stetho
        if (isDebug()) {
            Stetho.initializeWithDefaults(this);
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        // add multidex support
        MultiDex.install(this);
    }

    public static synchronized Context context() {
        return _context;
    }

    public static synchronized Resources resource() {
        return _resource;
    }

    protected boolean isProcessInRunning() {
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appList = activityManager.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : appList) {
            if (info.pid == android.os.Process.myPid()) {
                return true;
            }
        }
        return false;
    }

    protected boolean isDebug() {
        return true;
    }

    @Override
    public void onLowMemory() {
        super.onLowMemory();
        // auto gc when the memory is low
        System.gc();
    }

}

@@ -166,16 +166,42 @@
 ources;%0A
+import android.os.Looper;%0A
 import a
@@ -235,16 +235,49 @@
 tiDex;%0A%0A
+import com.bumptech.glide.Glide;%0A
 import c
@@ -2095,32 +2095,526 @@
 rn true;%0A    %7D%0A%0A
+    /**%0A     * clear glide cache%0A     */%0A    public boolean clearGlideCache() %7B%0A        try %7B%0A            if (Looper.myLooper() == Looper.getMainLooper()) %7B%0A                new Thread(() -%3E Glide.get(_context).clearDiskCache());%0A            %7D else %7B%0A                Glide.get(_context).clearDiskCache();%0A            %7D%0A            Glide.get(this).clearMemory();%0A            return true;%0A        %7D catch (Exception e) %7B%0A            e.printStackTrace();%0A            return false;%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

add mipush