Android Open Source - mobilepower-android App Utils






From Project

Back to project page mobilepower-android.

License

The source code is released under:

Apache License

If you think the Android project mobilepower-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2013 Dario Scoppelletti, <http://www.scoppelletti.it/>.
 * //ww  w .  j av  a2 s . c om
 * 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 it.scoppelletti.mobilepower.app;

import android.app.*;
import android.content.*;
import android.content.pm.*;
import android.net.*;
import android.os.*;
import android.view.*;
import android.view.inputmethod.*;
import org.apache.commons.lang3.*;
import org.slf4j.*;
import it.scoppelletti.mobilepower.os.*;

/**
 * Funzioni di utilit&agrave; sul modello delle applicazioni.
 * 
 * @since 1.0
 */
public final class AppUtils {

    /**
     * Metadato di applicazione <CODE>{@value}</CODE>: pacchetto della versione
     * completa dell&rsquo;App. 
     */
    public static final String METADATA_FULLPACKAGE =
            "it.scoppelletti.mobilepower.app.fullPackage";
   
    /**
     * Preferenza <CODE>{@value}</CODE>: Codice dell&rsquo;ultima versione
     * installata.
     */
    public static final String PREF_LASTVER =
            "it.scoppelletti.mobilepower.app.lastVer";
    
    private static final int Theme_Holo = 16973931;
    
    // - LG E-730/Android 2.3.4 
    // Il valore della costante DLG_THEME_TRADITIONAL sarebbe 1, ma con tale
    // valore, viene segnalato l'avviso "Invalid package identifier when getting
    // bag for resource number 0x00000001" (tag ResourceType).
    private static final int DLG_THEME_TRADITIONAL = 0;
    
    private static final int DLG_THEME_HOLO_DARK = 2;
    private static final int DLG_THEME_DEVICE_DEFAULT_DARK = 4;    
    private static final String APP_DOMAIN = "it.scoppelletti.mobilepower.";
    private static final Logger myLogger = LoggerFactory.getLogger("AppUtils");
    
    /**
     * Costruttore privato per classe statica.
     */
    private AppUtils() {        
    }
    
    /**
     * Restituisce il nome del pacchetto di un&rsquo;applicazione.
     * 
     * @param  ctx        Contesto.
     * @param  onlyIfDemo Indica se restituire il nome del pacchetto solo se
     *                    l&rsquo;applicazione &egrave; una versione di demo.
     * @return            Nome del pacchetto. Se il parametro {@code onlyIfDemo}
     *                    &egrave; impostato, pu&ograve; essere {@code null}.
     */
    static String getFullPackageName(Context ctx, boolean onlyIfDemo) {
        String pkgName, value;
        Bundle data;
        ApplicationInfo applInfo;
        PackageManager pkgMgr;
        
        if (ctx == null) {
            throw new NullPointerException("Argument ctx is null.");
        }
        
        pkgName = ctx.getPackageName();
        pkgMgr = ctx.getPackageManager();
        
        try {
            applInfo = pkgMgr.getApplicationInfo(pkgName,
                    PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException ex) {
            myLogger.error("Failed to get ApplicationInfo.", ex);
            applInfo = ctx.getApplicationInfo();            
        }        
        
        data = applInfo.metaData;
        value = (data != null) ?
                data.getString(AppUtils.METADATA_FULLPACKAGE) : null; 
        if (!StringUtils.isBlank(value)) {
            pkgName = value;
        } else if (onlyIfDemo) {
            return null;
        }
        
        return pkgName;
    }
    
    /**
     * Istanzia l&rsquo;URL per l&rsquo;acquisto di un&rsquo;applicazione.
     * 
     * @param  ctx  Contesto.
     * @param  rest Indica se comporre un URL REST-like senza parametri di
     *              interrogazione.
     * @return      URL.
     */
    public static Uri newBuyUri(Context ctx, boolean rest) {
        String pkgName;
        Uri.Builder uriBuilder;
        
        if (ctx == null) {
            throw new NullPointerException("Argument ctx is null.");
        }
        
        pkgName = AppUtils.getFullPackageName(ctx, false);
        
        uriBuilder = new Uri.Builder();
        if (rest) {
            if (pkgName.startsWith(AppUtils.APP_DOMAIN)) {
                pkgName = pkgName.substring(AppUtils.APP_DOMAIN.length());
            }
            
            uriBuilder.scheme("http")
                .authority("www.scoppelletti.it")
                .appendPath("mobilepower")
                .appendPath("android")
                .appendPath(pkgName);
        } else {
            uriBuilder.scheme("http")
                .authority("play.google.com")
                .appendPath("store")
                .appendPath("apps")
                .appendPath("details")
                .appendQueryParameter("id", pkgName);
        }
        
        return uriBuilder.build();
    }
    
    /**
     * Istanzia l&rsquo;intento per l&rsquo;acquisto di un&rsquo;applicazione.
     * 
     * @param  ctx Contesto.
     * @return     Intento.
     */
    public static Intent newBuyIntent(Context ctx) {
        String pkgName;
        Intent intent;
        Uri.Builder uriBuilder;
        
        if (ctx == null) {
            throw new NullPointerException("Argument ctx is null.");
        }
        
        pkgName = AppUtils.getFullPackageName(ctx, false);
        
        uriBuilder = new Uri.Builder();
        uriBuilder.scheme("market")
            .authority("details")
            .appendQueryParameter("id", pkgName);
        intent = new Intent(Intent.ACTION_VIEW, uriBuilder.build());
        
        return intent;
    }  
    
    /**
     * Restituisce il tema da applicare ad un&rsquo;attivit&agrave;.
     * 
     * @return Valore.
     */    
    public static int getActivityTheme() {
        if (Build.VERSION.SDK_INT >= BuildCompat.VERSION_CODES.HONEYCOMB) {
            return AppUtils.Theme_Holo;            
        }
        
        return android.R.style.Theme;
    }
        
    /**
     * Restituisce il tema da applicare ad un dialogo.
     * 
     * @return Valore.
     */
    public static int getDialogTheme() {
        if (Build.VERSION.SDK_INT >=
                BuildCompat.VERSION_CODES.ICE_CREAM_SANDWICH) {
            return AppUtils.DLG_THEME_DEVICE_DEFAULT_DARK;
        }        
        if (Build.VERSION.SDK_INT >= BuildCompat.VERSION_CODES.HONEYCOMB) {
            return AppUtils.DLG_THEME_HOLO_DARK;
        }
        
        return AppUtils.DLG_THEME_TRADITIONAL;
    }
    
    /**
     * Chiude la tastiera virtuale.
     * 
     * @param activity Attivit&agrave;.
     */
    public static void hideSoftInput(Activity activity) {
        View view;
        InputMethodManager inputMgr;
        
        if (activity == null) {
            throw new NullPointerException("Argument activity is null.");
        }
                
        view = activity.getCurrentFocus();
        if (view == null) {
            return;
        }
        
        inputMgr = (InputMethodManager) view.getContext().getSystemService(
                Activity.INPUT_METHOD_SERVICE);
        inputMgr.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }    
}




Java Source Code List

it.scoppelletti.mobilepower.app.AboutActivity.java
it.scoppelletti.mobilepower.app.AbstractActivity.java
it.scoppelletti.mobilepower.app.AbstractDialogFragment.java
it.scoppelletti.mobilepower.app.AbstractFragment.java
it.scoppelletti.mobilepower.app.AbstractPreferenceActivity.java
it.scoppelletti.mobilepower.app.ActionBarCompat.java
it.scoppelletti.mobilepower.app.ActionBarSupport.java
it.scoppelletti.mobilepower.app.ActivitySupport.java
it.scoppelletti.mobilepower.app.AppUtils.java
it.scoppelletti.mobilepower.app.CommonMenuFragment.java
it.scoppelletti.mobilepower.app.ConfirmDialogFragment.java
it.scoppelletti.mobilepower.app.DatePickerDialogFragment.java
it.scoppelletti.mobilepower.app.FragmentLayoutController.java
it.scoppelletti.mobilepower.app.FragmentSupport.java
it.scoppelletti.mobilepower.app.HelpActivity.java
it.scoppelletti.mobilepower.app.HelpDialogFragment.java
it.scoppelletti.mobilepower.app.MarketTagHandler.java
it.scoppelletti.mobilepower.app.ProgressDialogFragment.java
it.scoppelletti.mobilepower.app.ReleaseNoteActivity.java
it.scoppelletti.mobilepower.app.bluetooth.ActivityBTManager.java
it.scoppelletti.mobilepower.app.data.DatabaseConnectionManager.java
it.scoppelletti.mobilepower.app.data.DatabaseUpgradeTask.java
it.scoppelletti.mobilepower.app.security.ActivityLicenseClient.java
it.scoppelletti.mobilepower.app.security.LicenseBuyDialogFragment.java
it.scoppelletti.mobilepower.app.security.LicenseDemoDialogFragment.java
it.scoppelletti.mobilepower.app.security.LicenseRetryDialogFragment.java
it.scoppelletti.mobilepower.bluetooth.BTManager.java
it.scoppelletti.mobilepower.bluetooth.BTTask.java
it.scoppelletti.mobilepower.bluetooth.OnBTListener.java
it.scoppelletti.mobilepower.data.DatabaseUpgrader.java
it.scoppelletti.mobilepower.data.OnDatabaseConnectionListener.java
it.scoppelletti.mobilepower.graphics.GraphicTools.java
it.scoppelletti.mobilepower.graphics.SizeF.java
it.scoppelletti.mobilepower.io.IOTools.java
it.scoppelletti.mobilepower.media.DefaultOnScanCompletedListener.java
it.scoppelletti.mobilepower.os.AbstractAsyncTask.java
it.scoppelletti.mobilepower.os.AsyncTaskController.java
it.scoppelletti.mobilepower.os.AsyncTaskHost.java
it.scoppelletti.mobilepower.os.BuildCompat.java
it.scoppelletti.mobilepower.preference.AbstractDialogPreference.java
it.scoppelletti.mobilepower.preference.BTDevicePreference.java
it.scoppelletti.mobilepower.preference.ColorPreference.java
it.scoppelletti.mobilepower.preference.EditTextPreferenceEx.java
it.scoppelletti.mobilepower.preference.IntegerSpinnerPreference.java
it.scoppelletti.mobilepower.preference.PreferenceSavedState.java
it.scoppelletti.mobilepower.preference.SeekBarPreference.java
it.scoppelletti.mobilepower.provider.ContactsContractCompat.java
it.scoppelletti.mobilepower.reflect.MemberCache.java
it.scoppelletti.mobilepower.security.LicenseClient.java
it.scoppelletti.mobilepower.types.SimpleDate.java
it.scoppelletti.mobilepower.types.StringTools.java
it.scoppelletti.mobilepower.types.TimeTools.java
it.scoppelletti.mobilepower.types.ValueTools.java
it.scoppelletti.mobilepower.types.ValueType.java
it.scoppelletti.mobilepower.view.ViewSavedState.java
it.scoppelletti.mobilepower.widget.CompoundControl.java
it.scoppelletti.mobilepower.widget.DateControl.java
it.scoppelletti.mobilepower.widget.ImageButtonCompat.java
it.scoppelletti.mobilepower.widget.IntegerSpinner.java
it.scoppelletti.mobilepower.widget.IntegerTextWatcher.java
it.scoppelletti.mobilepower.widget.WidgetTools.java