You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
244 lines
8.8 KiB
244 lines
8.8 KiB
11 years ago
|
/*
|
||
|
* Copyright 2010-2013 Eric Kok et al.
|
||
|
*
|
||
|
* Transdroid 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, either version 3 of the License, or
|
||
|
* (at your option) any later version.
|
||
|
*
|
||
|
* Transdroid 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 Transdroid. If not, see <http://www.gnu.org/licenses/>.
|
||
|
*/
|
||
11 years ago
|
package org.transdroid.core.gui.navigation;
|
||
11 years ago
|
|
||
11 years ago
|
import android.annotation.SuppressLint;
|
||
11 years ago
|
import android.content.Context;
|
||
11 years ago
|
import android.content.pm.ApplicationInfo;
|
||
11 years ago
|
import android.content.pm.PackageInfo;
|
||
|
import android.content.pm.PackageManager.NameNotFoundException;
|
||
11 years ago
|
import android.net.Uri;
|
||
11 years ago
|
import android.text.Spannable;
|
||
|
import android.text.SpannableString;
|
||
|
import android.text.style.TypefaceSpan;
|
||
11 years ago
|
|
||
10 years ago
|
import com.nostra13.universalimageloader.cache.disc.impl.ext.LruDiscCache;
|
||
11 years ago
|
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
|
||
|
import com.nostra13.universalimageloader.cache.memory.impl.UsingFreqLimitedMemoryCache;
|
||
|
import com.nostra13.universalimageloader.core.DisplayImageOptions;
|
||
|
import com.nostra13.universalimageloader.core.ImageLoader;
|
||
|
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration.Builder;
|
||
|
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
|
||
|
|
||
10 years ago
|
import org.androidannotations.annotations.EBean;
|
||
|
import org.androidannotations.annotations.RootContext;
|
||
|
import org.transdroid.R;
|
||
|
|
||
|
import java.io.IOException;
|
||
|
|
||
11 years ago
|
import de.keyboardsurfer.android.widget.crouton.Crouton;
|
||
11 years ago
|
import de.keyboardsurfer.android.widget.crouton.Style;
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Helper for activities to make navigation-related decisions, such as when a device can display a larger, tablet style
|
||
|
* layout or how to display errors.
|
||
|
* @author Eric Kok
|
||
|
*/
|
||
|
@SuppressLint("ResourceAsColor")
|
||
11 years ago
|
@EBean
|
||
|
public class NavigationHelper {
|
||
|
|
||
|
@RootContext
|
||
|
protected Context context;
|
||
11 years ago
|
private Boolean inDebugMode;
|
||
11 years ago
|
private static ImageLoader imageCache;
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Use with {@link Crouton#showText(android.app.Activity, int, Style)} (and variants) to display error messages.
|
||
|
*/
|
||
10 years ago
|
public static Style CROUTON_ERROR_STYLE =
|
||
|
new Style.Builder().setBackgroundColor(R.color.crouton_error).setTextSize(13).build();
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Use with {@link Crouton#showText(android.app.Activity, int, Style)} (and variants) to display info messages.
|
||
|
*/
|
||
10 years ago
|
public static Style CROUTON_INFO_STYLE =
|
||
|
new Style.Builder().setBackgroundColor(R.color.crouton_info).setTextSize(13).build();
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Returns (and initialises, if needed) an image cache that uses memory and (1MB) local storage.
|
||
|
* @return An image cache that loads web images synchronously and transparently
|
||
|
*/
|
||
|
public ImageLoader getImageCache() {
|
||
|
if (imageCache == null) {
|
||
|
imageCache = ImageLoader.getInstance();
|
||
10 years ago
|
try {
|
||
|
LruDiscCache diskCache =
|
||
|
new LruDiscCache(context.getCacheDir(), null, new Md5FileNameGenerator(), 640000, 25);
|
||
|
// @formatter:off
|
||
|
Builder imageCacheBuilder = new Builder(context)
|
||
|
.defaultDisplayImageOptions(
|
||
|
new DisplayImageOptions.Builder()
|
||
|
.cacheInMemory()
|
||
|
.cacheOnDisc()
|
||
|
.imageScaleType(ImageScaleType.IN_SAMPLE_INT)
|
||
|
.showImageForEmptyUri(R.drawable.ic_launcher).build())
|
||
|
.memoryCache(new UsingFreqLimitedMemoryCache(1024 * 1024))
|
||
|
.discCache(diskCache);
|
||
|
imageCache.init(imageCacheBuilder.build());
|
||
11 years ago
|
// @formatter:on
|
||
10 years ago
|
} catch (IOException e) {
|
||
|
// The cache directory is always available on Android; ignore this exception
|
||
|
}
|
||
11 years ago
|
}
|
||
|
return imageCache;
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Whether any search-related UI components should be shown in the interface. At the moment returns false only if we
|
||
|
* run as Transdroid Lite version.
|
||
11 years ago
|
* @return True if search is enabled, false otherwise
|
||
|
*/
|
||
|
public String getAppNameAndVersion() {
|
||
|
String appName = context.getString(R.string.app_name);
|
||
|
try {
|
||
|
PackageInfo m = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
||
|
return appName + " " + m.versionName + " (" + m.versionCode + ")";
|
||
|
} catch (NameNotFoundException e) {
|
||
|
return appName;
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Returns whether the application is running in debug mode, as opposed to release mode. Use to show/hide features
|
||
|
* in the ui based on the build mode.
|
||
|
* @return True if the app is compiled in/running as debug mode, false otherwise
|
||
|
*/
|
||
|
public boolean inDebugMode() {
|
||
|
try {
|
||
|
if (inDebugMode == null) {
|
||
|
PackageInfo pi = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
|
||
|
inDebugMode = (pi.applicationInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
|
||
|
}
|
||
|
return inDebugMode;
|
||
|
} catch (NameNotFoundException e) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
11 years ago
|
* Returns whether the device is considered small (i.e. a phone) rather than large (i.e. a tablet). Can, for
|
||
|
* example, be used to determine if a dialog should be shown full screen. Currently is true if the device's smallest
|
||
11 years ago
|
* dimension is 500 dip.
|
||
|
* @return True if the app runs on a small device, false otherwise
|
||
|
*/
|
||
|
public boolean isSmallScreen() {
|
||
11 years ago
|
return context.getResources().getBoolean(R.bool.show_dialog_fullscreen);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Whether any search-related UI components should be shown in the interface. At the moment returns false only if we
|
||
|
* run as Transdroid Lite version.
|
||
11 years ago
|
* @return True if search is enabled, false otherwise
|
||
|
*/
|
||
|
public boolean enableSearchUi() {
|
||
11 years ago
|
return context.getResources().getBoolean(R.bool.search_available);
|
||
11 years ago
|
}
|
||
|
|
||
|
/**
|
||
|
* Whether any RSS-related UI components should be shown in the interface. At the moment returns false only if we
|
||
|
* run as Transdroid Lite version.
|
||
|
* @return True if search is enabled, false otherwise
|
||
|
*/
|
||
|
public boolean enableRssUi() {
|
||
11 years ago
|
return context.getResources().getBoolean(R.bool.rss_available);
|
||
|
}
|
||
|
|
||
|
/**
|
||
10 years ago
|
* Returns whether any seedbox-related components should be shown in the interface; specifically the option to add
|
||
11 years ago
|
* server settings via easy seedbox-specific screens.
|
||
|
* @return True if seedbox settings should be shown, false otherwise
|
||
|
*/
|
||
|
public boolean enableSeedboxes() {
|
||
|
return context.getResources().getBoolean(R.bool.seedboxes_available);
|
||
11 years ago
|
}
|
||
11 years ago
|
|
||
11 years ago
|
/**
|
||
|
* Whether the custom app update checker should be used to check for new app and search module versions.
|
||
|
* @return True if it should be checked against transdroid.org if there are app updates (as opposed to using the
|
||
10 years ago
|
* Play Store for updates, for example), false otherwise
|
||
11 years ago
|
*/
|
||
|
public boolean enableUpdateChecker() {
|
||
|
return context.getResources().getBoolean(R.bool.updatecheck_available);
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
|
* Converts a string into a {@link Spannable} that displays the string in the Roboto Condensed font
|
||
|
* @param string A plain text {@link String}
|
||
|
* @return A {@link Spannable} that can be applied to supporting views (such as the action bar title) so that the
|
||
10 years ago
|
* input string will be displayed using the Roboto Condensed font (if the OS has this)
|
||
11 years ago
|
*/
|
||
|
public static SpannableString buildCondensedFontString(String string) {
|
||
10 years ago
|
if (string == null) {
|
||
11 years ago
|
return null;
|
||
10 years ago
|
}
|
||
11 years ago
|
SpannableString s = new SpannableString(string);
|
||
|
s.setSpan(new TypefaceSpan("sans-serif-condensed"), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
|
||
|
return s;
|
||
|
}
|
||
|
|
||
11 years ago
|
/**
|
||
10 years ago
|
* Analyses a torrent http or magnet URI and tries to come up with a reasonable human-readable name.
|
||
11 years ago
|
* @param rawTorrentUri The raw http:// or magnet: link to the torrent
|
||
|
* @return A best-guess, reasonably long name for the linked torrent
|
||
|
*/
|
||
|
public static String extractNameFromUri(Uri rawTorrentUri) {
|
||
11 years ago
|
|
||
11 years ago
|
if (rawTorrentUri.getScheme() == null) {
|
||
|
// Probably an incorrect URI; just return the whole thing
|
||
|
return rawTorrentUri.toString();
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
if (rawTorrentUri.getScheme().equals("magnet")) {
|
||
|
// Magnet links might have a dn (display name) parameter
|
||
|
String dn = getQueryParameter(rawTorrentUri, "dn");
|
||
10 years ago
|
if (dn != null && !dn.equals("")) {
|
||
11 years ago
|
return dn;
|
||
10 years ago
|
}
|
||
11 years ago
|
// If not, try to return the hash that is specified as xt (exact topci)
|
||
|
String xt = getQueryParameter(rawTorrentUri, "xt");
|
||
10 years ago
|
if (xt != null && !xt.equals("")) {
|
||
11 years ago
|
return xt;
|
||
10 years ago
|
}
|
||
11 years ago
|
}
|
||
|
|
||
|
if (rawTorrentUri.isHierarchical()) {
|
||
|
String path = rawTorrentUri.getPath();
|
||
|
if (path != null) {
|
||
10 years ago
|
if (path.contains("/")) {
|
||
11 years ago
|
path = path.substring(path.lastIndexOf("/"));
|
||
10 years ago
|
}
|
||
11 years ago
|
return path;
|
||
|
}
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
// No idea what to do with this; return as is
|
||
|
return rawTorrentUri.toString();
|
||
|
}
|
||
|
|
||
|
private static String getQueryParameter(Uri uri, String parameter) {
|
||
|
int start = uri.toString().indexOf(parameter + "=");
|
||
|
if (start >= 0) {
|
||
|
int begin = start + (parameter + "=").length();
|
||
|
int end = uri.toString().indexOf("&", begin);
|
||
11 years ago
|
return uri.toString().substring(begin, end >= 0 ? end : uri.toString().length());
|
||
11 years ago
|
}
|
||
|
return null;
|
||
|
}
|
||
11 years ago
|
|
||
11 years ago
|
}
|