/*
 * Copyright (c) 2016 Kiall Mac Innes <kiall@macinnes.ie>
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License. You may obtain
 * a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package ie.macinnes.tvheadend;

import android.content.Context;
import android.util.Log;

import com.squareup.leakcanary.LeakCanary;
import com.squareup.leakcanary.RefWatcher;

import org.acra.ACRA;
import org.acra.config.ACRAConfiguration;
import org.acra.config.ACRAConfigurationException;
import org.acra.config.ConfigurationBuilder;
import org.acra.sender.HttpSender;

import ie.macinnes.tvheadend.migrate.MigrateUtils;

public class Application extends android.app.Application {
    private static final String TAG = Application.class.getName();

    private RefWatcher mRefWatcher;

    public static RefWatcher getRefWatcher(Context context) {
        Application application = (Application) context.getApplicationContext();
        return application.mRefWatcher;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        if (!LeakCanary.isInAnalyzerProcess(this)) {
            mRefWatcher = LeakCanary.install(this);
        }
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);

        // Initialize ACRA crash reporting
        if (BuildConfig.ACRA_ENABLED) {
            Log.i(TAG, "Initializing ACRA");
            try {
                final ACRAConfiguration config = new ConfigurationBuilder(this)
                        .setHttpMethod(HttpSender.Method.PUT)
                        .setReportType(HttpSender.Type.JSON)
                        .setFormUri(BuildConfig.ACRA_REPORT_URI + "/" + BuildConfig.VERSION_CODE)
                        .setLogcatArguments("-t", "1000", "-v", "time", "*:D")
                        .setAdditionalSharedPreferences(Constants.PREFERENCE_TVHEADEND)
                        .setSharedPreferenceName(Constants.PREFERENCE_TVHEADEND)
                        .setSharedPreferenceMode(Context.MODE_PRIVATE)
                        .setBuildConfigClass(BuildConfig.class)
                        .build();
                ACRA.init(this, config);
            } catch (ACRAConfigurationException e) {
                Log.e(TAG, "Failed to init ACRA", e);
            }
        }

        // TODO: Find a better (+ out of UI thread) way to do this.
        MigrateUtils.doMigrate(getBaseContext());
    }
}

@@ -677,16 +677,104 @@
 ontext;%0A
+import android.net.TrafficStats;%0Aimport android.os.Build;%0Aimport android.os.StrictMode;%0A
 import a
@@ -2933,16 +2933,1240 @@
 text());
+%0A%0A        if (BuildConfig.DEBUG) %7B%0A            // StrictMode Setup%0A            Log.i(TAG, %22Initializing Android StrictMode%22);%0A            StrictMode.ThreadPolicy.Builder threadPolicy = new StrictMode.ThreadPolicy.Builder()%0A                    .detectAll()%0A                    .penaltyLog()%0A                    .penaltyDeath();%0A%0A            StrictMode.setThreadPolicy(threadPolicy.build());%0A%0A            StrictMode.VmPolicy.Builder vmPolicy = new StrictMode.VmPolicy.Builder()%0A                    .detectAll()%0A                    .detectActivityLeaks()%0A                    .detectLeakedClosableObjects()%0A                    .detectLeakedRegistrationObjects()%0A                    .detectLeakedSqlLiteObjects()%0A                    .detectFileUriExposure()%0A//                    .detectUntaggedSockets()  // Skipped as our HTSP lib does not tag its sockets yet%0A//                    .detectCleartextNetwork() // Skipped as HTSP is not encrypted....%0A                    .penaltyLog()%0A                    .penaltyDeath();%0A%0A%0A            if (Build.VERSION.SDK_INT %3E= Build.VERSION_CODES.O) %7B%0A                vmPolicy.detectContentUriWithoutPermission();%0A            %7D%0A%0A            StrictMode.setVmPolicy(vmPolicy.build());%0A        %7D
 %0A    %7D%0A%7D

Enable as many StrictMode checks as possible for Debug builds