free.rm.skytube.app.SkyTubeApp.java Source code

Java tutorial

Introduction

Here is the source code for free.rm.skytube.app.SkyTubeApp.java

Source

/*
 * SkyTube
 * Copyright (C) 2015  Ramon Mifsud
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation (version 3 of the License).
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package free.rm.skytube.app;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.preference.PreferenceManager;
import android.support.multidex.MultiDexApplication;
import android.support.v4.content.IntentCompat;

import java.util.Arrays;
import java.util.List;

/**
 * SkyTube application.
 */
public class SkyTubeApp extends MultiDexApplication {

    /** SkyTube Application databaseInstance. */
    private static SkyTubeApp skyTubeApp = null;

    public static final String KEY_SUBSCRIPTIONS_LAST_UPDATED = "SkyTubeApp.KEY_SUBSCRIPTIONS_LAST_UPDATED";
    public static String KEY_SET_UPDATE_FEED_TAB = "SkyTubeApp.KEY_SET_UPDATE_FEED_TAB";

    @Override
    public void onCreate() {
        super.onCreate();
        skyTubeApp = this;
    }

    /**
     * Returns a localised string.
     *
     * @param  stringResId   String resource ID (e.g. R.string.my_string)
     * @return Localised string, from the strings XML file.
     */
    public static String getStr(int stringResId) {
        return skyTubeApp.getString(stringResId);
    }

    /**
     * Given a string array resource ID, it returns an array of strings.
     *
     * @param stringArrayResId String array resource ID (e.g. R.string.my_array_string)
     * @return Array of String.
     */
    public static String[] getStringArray(int stringArrayResId) {
        return skyTubeApp.getResources().getStringArray(stringArrayResId);
    }

    /**
     * Given a string array resource ID, it returns an list of strings.
     *
     * @param stringArrayResId String array resource ID (e.g. R.string.my_array_string)
     * @return List of String.
     */
    public static List<String> getStringArrayAsList(int stringArrayResId) {
        return Arrays.asList(getStringArray(stringArrayResId));
    }

    /**
     * Returns the App's {@link SharedPreferences}.
     *
     * @return {@link SharedPreferences}
     */
    public static SharedPreferences getPreferenceManager() {
        return PreferenceManager.getDefaultSharedPreferences(skyTubeApp);
    }

    /**
     * Returns the dimension value that is specified in R.dimens.*.  This value is NOT converted into
     * pixels, but rather it is kept as it was originally written (e.g. dp).
     *
     * @return The dimension value.
     */
    public static float getDimension(int dimensionId) {
        return skyTubeApp.getResources().getDimension(dimensionId);
    }

    /**
     * @return {@link Context}.
     */
    public static Context getContext() {
        return skyTubeApp.getBaseContext();
    }

    /**
     * Restart the app.
     */
    public static void restartApp() {
        Context context = getContext();
        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
        ComponentName componentName = intent.getComponent();
        Intent mainIntent = IntentCompat.makeRestartActivityTask(componentName);
        context.startActivity(mainIntent);
        System.exit(0);
    }

}