Learn more  » Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Bower components Debian packages RPM packages NuGet packages

vipera-npm-registry / de-splashscreen-plugin   js

Repository URL to install this package:

Version: 0.0.4 

/ src / android / DESplashScreenHandler.java

package com.vipera.splashscreen;

import android.graphics.Color;
import android.os.Looper;

import com.vipera.de.utility.logging.impl.DELoggerFactory;

import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaPreferences;
import org.slf4j.Logger;

/**
 * Created by enrico on 23/02/17.
 */

public class DESplashScreenHandler implements DESplashScreen {
    private static final int DEFAULT_FADE_DURATION = 500;
    private static final String BACKGROUND_COLOR_PREF_TAG = "desplash.backgroundColor";
    private static final String FADE_DURATION_PREF_TAG = "desplash.transition.fade.duration";
    private static final String DESTROY_ON_HIDE_PREF_TAG = "desplash.destroyOnHide";
    public static final String SPLASH_DRAWABLE_ID_PREF_TAG = "SplashDrawableId";

    private static final Logger LOGGER= DELoggerFactory.getLogger(DESplashScreenHandler.class);
    private static final String DEFAULT_BACKGROUND_COLOR = "#000000";
    public static final String DE_SPLASH_SCREEN_RES_NAME_PREF_TAG = "desplash.resourceName";
    public static final String DEFAULT_SCREEN_RES_NAME = "screen";

    private BaseSplashScreen splashScreen;
    private CordovaInterface cordova;
    private CordovaPreferences preferences;

    public DESplashScreenHandler(CordovaInterface cordova, CordovaPreferences preferences){
        this.cordova=cordova;
        this.preferences=preferences;
        patchDrawableId();
        splashScreen = makeSplashScreen();
    }

    private BaseSplashScreen makeSplashScreen() {
        int drawableId = preferences.getInteger(SPLASH_DRAWABLE_ID_PREF_TAG, 0);
        boolean destroyOnHide=preferences.getBoolean(DESTROY_ON_HIDE_PREF_TAG,false);
        BaseSplashScreen splashScreen = new BaseSplashScreen(cordova,drawableId,destroyOnHide);
        splashScreen.setFadeDuration(preferences.getInteger(FADE_DURATION_PREF_TAG,DEFAULT_FADE_DURATION));
        splashScreen.setBackgroundColor(preferences.getString(BACKGROUND_COLOR_PREF_TAG, DEFAULT_BACKGROUND_COLOR));
        return splashScreen;
    }


    private void patchDrawableId(){
        int drawableId = preferences.getInteger("SplashDrawableId", 0);
        LOGGER.debug("drawableId: {}",drawableId);
        if (drawableId == 0) {
            String splashResource = preferences.getString(DE_SPLASH_SCREEN_RES_NAME_PREF_TAG, DEFAULT_SCREEN_RES_NAME);
            LOGGER.debug("splashResourceName: {}",splashResource);
            if (splashResource != null) {
                drawableId = cordova.getActivity().getResources().getIdentifier(splashResource, "drawable", cordova.getActivity().getClass().getPackage().getName());
                LOGGER.debug("new drawableId: {}",drawableId);
                if (drawableId == 0) {
                    drawableId = cordova.getActivity().getResources().getIdentifier(splashResource, "drawable", cordova.getActivity().getPackageName());
                    LOGGER.debug("last drawableId: {}",drawableId);
                }
                preferences.set(SPLASH_DRAWABLE_ID_PREF_TAG, drawableId);
            }
        }
    }
    @Override
    public void show() {
        LOGGER.debug("show");
        if(isInMainThread()){
            splashScreen.show();
        }else{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    splashScreen.show();
                }
            });
        }
    }
    @Override
    public void hide(){
        LOGGER.debug("hide");
        if(isInMainThread()){
            splashScreen.hide();
        }else{
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    splashScreen.hide();
                }
            });
        }
    }
    @Override
    public void destroy() {
        LOGGER.debug("destroy");
        splashScreen = null;
        cordova = null;
        //splashScreen.destroy();
    }

    private boolean isInMainThread(){
        return Looper.myLooper() == Looper.getMainLooper();
    }

    private void runOnUiThread(Runnable runnable){
        cordova.getActivity().runOnUiThread(runnable);
    }
}