Back to project page TATupload.
The source code is released under:
GNU General Public License
If you think the Android project TATupload listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package uk.org.sucu.tatupload; // w w w. ja va 2 s. c o m import java.util.List; import android.annotation.SuppressLint; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.net.Uri; import android.view.LayoutInflater; import android.view.View; import android.widget.Spinner; public class BrowserAccessor { public static List<ResolveInfo> getResolvers(Context context){ //build an intent that launches to the webscript url Uri uri = Uri.parse(context.getString(R.string.scriptURL)); Intent browserIntent = new Intent(Intent.ACTION_VIEW); browserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); browserIntent.setData(uri); //return a list of the packages that can handle the intent return context.getPackageManager().queryIntentActivities(browserIntent, PackageManager.MATCH_DEFAULT_ONLY); } public static String getPackageName(ResolveInfo resolveInfo){ return resolveInfo.activityInfo.packageName; } public static String getName(ResolveInfo resolveInfo){ return resolveInfo.activityInfo.name; } public static boolean browserSet(Context context){ Settings settings = new Settings(context); String browserPackageName = settings.getChosenBrowserPackage(); String browserName = settings.getChosenBrowserName(); //null is returned as default if no saved browser package name is able to be loaded return ((browserPackageName != null) && (browserName != null)); } public static boolean isInstalled(String packageName, String className, Context context){ //get a list of all installed packages List<ResolveInfo> apps = getResolvers(context); //return true if one has the desired class and packageName for(ResolveInfo app : apps){ if(getPackageName(app).equals(packageName) && getName(app).equals(className)){ return true; } } //else return false return false; } public static void openBrowserChoicePopup(Context context, boolean cancelable){ openBrowserChoicePopup(context, cancelable, null); } @SuppressLint("InflateParams") public static void openBrowserChoicePopup(final Context context, boolean cancelable, final Runnable onCloseCode){ final View viewToLoad = LayoutInflater.from(context).inflate(R.layout.browser_choice_popup, null); AlertDialog.Builder builder = new AlertDialog.Builder(context) .setTitle("Select browser") .setView(viewToLoad) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //get the selected ResolveInfo Spinner spin = (Spinner) viewToLoad.findViewById(R.id.browserSpinner); ResolveInfo selected = (ResolveInfo) spin.getSelectedItem(); //get the packagename String packageName = getPackageName(selected); String className = getName(selected); //save it new Settings(context).setBrowserData(packageName, className); if(onCloseCode != null){ onCloseCode.run(); } } }) .setCancelable(cancelable); //add a cancel button if applicable if(cancelable){ builder.setNegativeButton(android.R.string.cancel, null); } builder.create(); List<ResolveInfo> resolvers = getResolvers(context); //connect the dialogs spinner to the data structure Spinner spin = (Spinner) viewToLoad.findViewById(R.id.browserSpinner); ResolveInfoArrayAdapter adapter = new ResolveInfoArrayAdapter(context, android.R.layout.simple_spinner_item, resolvers); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spin.setAdapter(adapter); builder.show(); } public static boolean usable(Context context){ //load the browser data Settings settings = new Settings(context); String packageName = settings.getChosenBrowserPackage(); String className = settings.getChosenBrowserName(); //check they're actual saved values, not the defaults from failure if(packageName == Settings.BROWSER_PACKAGE_DEFAULT || className == Settings.BROWSER_NAME_DEFAULT){ return false; } //check it's still installed return isInstalled(packageName, className, context); } }