package cat.xojan.random1;

import android.content.Context;
import android.os.Environment;

import com.crashlytics.android.Crashlytics;
import com.crashlytics.android.answers.Answers;
import com.squareup.leakcanary.LeakCanary;

import java.io.File;

import cat.xojan.random1.commons.Log;
import cat.xojan.random1.injection.component.AppComponent;
import cat.xojan.random1.injection.component.DaggerAppComponent;
import cat.xojan.random1.injection.module.AppModule;
import io.fabric.sdk.android.Fabric;

public class Application extends android.app.Application {

    private static final String TAG = Application.class.getSimpleName();
    protected AppComponent mAppComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        initCrashlytics();
        initInjector();
        initLeakDetection();

        removePreviousDownloadedPodcasts();
    }

    public AppComponent getAppComponent() {
        return mAppComponent;
    }

    private void initInjector() {
        mAppComponent = DaggerAppComponent.builder()
                .appModule(new AppModule(this))
                .build();
    }

    private void initLeakDetection() {
        if (BuildConfig.DEBUG) {
            LeakCanary.install(this);
        }
    }

    private void initCrashlytics() {
        if (!BuildConfig.DEBUG) {
            Fabric.with(this, new Answers(), new Crashlytics());
        }
    }

    private void removePreviousDownloadedPodcasts() {
        boolean arePodcastsRemoved = getSharedPreferences("preferences", Context.MODE_PRIVATE)
                .getBoolean("old_downloaded_podcasts", false);
        if (!arePodcastsRemoved) {
            getSharedPreferences("preferences", Context.MODE_PRIVATE).edit()
                    .putBoolean("old_downloaded_podcasts", true).apply();
            Log.d(TAG, "removing old podcasts");

            removeDownloaded();
            removeDownloading();
        }
    }

    private void removeDownloaded() {
        File iternalFileDirectory = getExternalFilesDir(Environment.DIRECTORY_PODCASTS);
        for (File podcastFile : iternalFileDirectory.listFiles()) {
            if (podcastFile.delete()) {
                Log.d(TAG, "delete downloaded podcast");
            }
        }
    }

    private void removeDownloading() {
        File iternalFileDirectory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
        for (File podcastFile : iternalFileDirectory.listFiles()) {
            if (podcastFile.delete()) {
                Log.d(TAG, "delete downloading podcast");
            }
        }
    }
}

@@ -25,72 +25,8 @@
 1;%0A%0A
-import android.content.Context;%0Aimport android.os.Environment;%0A%0A
 impo
@@ -65,16 +65,16 @@
 lytics;%0A
+
 import c
@@ -161,68 +161,8 @@
 y;%0A%0A
-import java.io.File;%0A%0Aimport cat.xojan.random1.commons.Log;%0A
 impo
@@ -436,81 +436,8 @@
  %7B%0A%0A
-    private static final String TAG = Application.class.getSimpleName();%0A
     
@@ -627,53 +627,8 @@
 n();
-%0A%0A        removePreviousDownloadedPodcasts();
 %0A   
@@ -1070,32 +1070,32 @@
 Config.DEBUG) %7B%0A
+
             Fabr
@@ -1162,1186 +1162,7 @@
    %7D
-%0A%0A    private void removePreviousDownloadedPodcasts() %7B%0A        boolean arePodcastsRemoved = getSharedPreferences(%22preferences%22, Context.MODE_PRIVATE)%0A                .getBoolean(%22old_downloaded_podcasts%22, false);%0A        if (!arePodcastsRemoved) %7B%0A            getSharedPreferences(%22preferences%22, Context.MODE_PRIVATE).edit()%0A                    .putBoolean(%22old_downloaded_podcasts%22, true).apply();%0A            Log.d(TAG, %22removing old podcasts%22);%0A%0A            removeDownloaded();%0A            removeDownloading();%0A        %7D%0A    %7D%0A%0A    private void removeDownloaded() %7B%0A        File iternalFileDirectory = getExternalFilesDir(Environment.DIRECTORY_PODCASTS);%0A        for (File podcastFile : iternalFileDirectory.listFiles()) %7B%0A            if (podcastFile.delete()) %7B%0A                Log.d(TAG, %22delete downloaded podcast%22);%0A            %7D%0A        %7D%0A    %7D%0A%0A    private void removeDownloading() %7B%0A        File iternalFileDirectory = getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);%0A        for (File podcastFile : iternalFileDirectory.listFiles()) %7B%0A            if (podcastFile.delete()) %7B%0A                Log.d(TAG, %22delete downloading podcast%22);%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

remove temporary code