All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gluonhq.charm.down.android.AndroidPlatform Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2015, Gluon
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 *     * Neither the name of Gluon, any associated website, nor the
 * names of its contributors may be used to endorse or promote products
 * derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.gluonhq.charm.down.android;

import android.app.Activity;
import android.app.Application.ActivityLifecycleCallbacks;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import com.gluonhq.charm.down.android.ble.BleServiceImpl;
import com.gluonhq.charm.down.android.notifications.AndroidNotificationsService;
import com.gluonhq.charm.down.android.scan.ScanServiceImpl;
import com.gluonhq.charm.down.common.BleService;
import com.gluonhq.charm.down.common.NotificationsService;
import com.gluonhq.charm.down.common.PicturesService;
import com.gluonhq.charm.down.common.Platform;
import com.gluonhq.charm.down.common.PlatformFactory;
import com.gluonhq.charm.down.common.PositionService;
import com.gluonhq.charm.down.common.ScanService;
import com.gluonhq.charm.down.common.SettingService;
import com.gluonhq.charm.down.common.cache.CacheManager;
import javafx.geometry.Orientation;
import javafx.util.Callback;
import javafxports.android.FXActivity;

import java.io.File;
import java.io.IOException;
import java.util.Optional;

/**
 *
 * @author johan
 */
public class AndroidPlatform extends Platform {

    private AndroidNotificationsService androidNotificationService = null;

    static FXActivity activity;
    static {
        activity = FXActivity.getInstance();
    }

    private AndroidPositionService androidPositionService;
    private AndroidSettingService androidSettingService;
    private ScanService scanService;
    private AndroidPicturesService picturesService;

    @Override
    public String getName() {
        return PlatformFactory.ANDROID;
    }
    
    @Override
    public File getPrivateStorage() throws IOException {
        return activity.getFilesDir();
    }

    @Override
    public PositionService getPositionService() {
        if (androidPositionService == null) {
            androidPositionService = new AndroidPositionService();
        }
        return androidPositionService;
    }

    @Override
    public SettingService getSettingService() {
        if (androidSettingService == null) {
            androidSettingService = new AndroidSettingService();
        }
        return androidSettingService;
    }
    
    @Override
    public BleService getBleService(){
        return new BleServiceImpl();
    }

    @Override
    public void setOnLifecycleEvent(Callback callback) {
        activity.getApplication().registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity actvt, Bundle bundle) {
            }

            @Override
            public void onActivityStarted(Activity actvt) {
                callback.call(LifecycleEvent.START);
            }

            @Override
            public void onActivityResumed(Activity actvt) {
                callback.call(LifecycleEvent.RESUME);
            }

            @Override
            public void onActivityPaused(Activity actvt) {
                callback.call(LifecycleEvent.PAUSE);
            }

            @Override
            public void onActivityStopped(Activity actvt) {
                callback.call(LifecycleEvent.STOP);
            }

            @Override
            public void onActivitySaveInstanceState(Activity actvt, Bundle bundle) {
            }

            @Override
            public void onActivityDestroyed(Activity actvt) {
            }
        });
    }
    
    @Override
    public void launchExternalBrowser(String url) {
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        activity.startActivity(browserIntent);
    }
    
    @Override
    public CacheManager getCacheManager() {
        return new AndroidCacheManager();
    }

    @Override
    public Optional getOrientation() {

        switch ( activity.getResources().getConfiguration().orientation ) {
            case Configuration.ORIENTATION_LANDSCAPE: return Optional.of(Orientation.HORIZONTAL);
            case Configuration.ORIENTATION_PORTRAIT: return Optional.of(Orientation.VERTICAL);
            default: return Optional.empty();
        }

    }

    @Override
    public void finish() {
        FXActivity.getInstance().finish();
    }

    @Override
    public boolean isTablet() {
        DisplayMetrics metrics = new DisplayMetrics();
        FXActivity.getInstance().getWindowManager().getDefaultDisplay().getMetrics(metrics);

        try {
            float yInches = metrics.heightPixels / metrics.ydpi;
            float xInches = metrics.widthPixels / metrics.xdpi;
            double diagonalInches = Math.sqrt(xInches * xInches + yInches * yInches);
            return (diagonalInches >= 6.5);
        } catch(Exception e) {
            return false;
        }
    }

    @Override
    public NotificationsService getNotificationsService() {
        if (androidNotificationService == null) {
            androidNotificationService = new AndroidNotificationsService();
        }
        return androidNotificationService;
    }
    
    @Override
    public ScanService getScanService() {
        if (scanService == null) {
            scanService = new ScanServiceImpl();
        }
        return scanService;
    }
    
    @Override
    public PicturesService getPicturesService() {
        if (picturesService == null) {
            picturesService = new AndroidPicturesService();
        }
        return picturesService;
    }
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy