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.
215 lines
6.2 KiB
215 lines
6.2 KiB
10 years ago
|
/*
|
||
|
* Copyright 2014 A.C.R. Development
|
||
|
*/
|
||
|
package acr.browser.lightning;
|
||
|
|
||
9 years ago
|
import android.annotation.SuppressLint;
|
||
10 years ago
|
import android.app.Activity;
|
||
|
import android.app.AlertDialog;
|
||
|
import android.content.Context;
|
||
|
import android.content.DialogInterface;
|
||
|
import android.content.Intent;
|
||
9 years ago
|
import android.content.res.Resources;
|
||
9 years ago
|
import android.graphics.Bitmap;
|
||
9 years ago
|
import android.graphics.BitmapFactory;
|
||
9 years ago
|
import android.graphics.Canvas;
|
||
|
import android.graphics.Paint;
|
||
9 years ago
|
import android.os.Environment;
|
||
9 years ago
|
import android.util.DisplayMetrics;
|
||
10 years ago
|
import android.util.Log;
|
||
|
import android.webkit.URLUtil;
|
||
|
import android.widget.Toast;
|
||
|
|
||
10 years ago
|
import java.io.*;
|
||
|
import java.net.URI;
|
||
|
import java.net.URISyntaxException;
|
||
9 years ago
|
import java.text.SimpleDateFormat;
|
||
10 years ago
|
import java.util.ArrayList;
|
||
9 years ago
|
import java.util.Date;
|
||
10 years ago
|
import java.util.List;
|
||
|
|
||
10 years ago
|
public final class Utils {
|
||
|
|
||
|
private Utils() {
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static void downloadFile(final Activity activity, final String url,
|
||
|
final String userAgent, final String contentDisposition, final boolean privateBrowsing) {
|
||
|
String fileName = URLUtil.guessFileName(url, null, null);
|
||
|
DownloadHandler.onDownloadStart(activity, url, userAgent, contentDisposition, null,
|
||
|
privateBrowsing);
|
||
10 years ago
|
Log.i(Constants.TAG, "Downloading" + fileName);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static Intent newEmailIntent(Context context, String address, String subject,
|
||
|
String body, String cc) {
|
||
10 years ago
|
Intent intent = new Intent(Intent.ACTION_SEND);
|
||
10 years ago
|
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { address });
|
||
10 years ago
|
intent.putExtra(Intent.EXTRA_TEXT, body);
|
||
|
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
|
||
|
intent.putExtra(Intent.EXTRA_CC, cc);
|
||
|
intent.setType("message/rfc822");
|
||
|
return intent;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static void createInformativeDialog(Context context, String title, String message) {
|
||
10 years ago
|
AlertDialog.Builder builder = new AlertDialog.Builder(context);
|
||
|
builder.setTitle(title);
|
||
10 years ago
|
builder.setMessage(message)
|
||
|
.setCancelable(true)
|
||
10 years ago
|
.setPositiveButton(context.getResources().getString(R.string.action_ok),
|
||
|
new DialogInterface.OnClickListener() {
|
||
|
@Override
|
||
|
public void onClick(DialogInterface dialog, int id) {
|
||
|
}
|
||
|
});
|
||
10 years ago
|
AlertDialog alert = builder.create();
|
||
|
alert.show();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static void showToast(Context context, String message) {
|
||
|
Toast.makeText(context, message, Toast.LENGTH_SHORT).show();
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
/**
|
||
|
* Returns the number of pixels corresponding to the passed density pixels
|
||
|
*/
|
||
9 years ago
|
public static int convertDpToPixels(int dp) {
|
||
|
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
|
||
9 years ago
|
return (int) (dp * metrics.density + 0.5f);
|
||
10 years ago
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static String getDomainName(String url) {
|
||
9 years ago
|
boolean ssl = url.startsWith(Constants.HTTPS);
|
||
|
int index = url.indexOf('/', 8);
|
||
|
if (index != -1) {
|
||
|
url = url.substring(0, index);
|
||
|
}
|
||
|
|
||
10 years ago
|
URI uri;
|
||
9 years ago
|
String domain = null;
|
||
10 years ago
|
try {
|
||
|
uri = new URI(url);
|
||
9 years ago
|
domain = uri.getHost();
|
||
10 years ago
|
} catch (URISyntaxException e) {
|
||
9 years ago
|
e.printStackTrace();
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
if (domain == null || domain.isEmpty()) {
|
||
10 years ago
|
return url;
|
||
|
}
|
||
9 years ago
|
if (ssl)
|
||
|
return Constants.HTTPS + domain;
|
||
|
else
|
||
|
return domain.startsWith("www.") ? domain.substring(4) : domain;
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
9 years ago
|
public static String getProtocol(String url) {
|
||
|
int index = url.indexOf('/');
|
||
|
return url.substring(0, index + 2);
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static List<HistoryItem> getOldBookmarks(Context context) {
|
||
10 years ago
|
List<HistoryItem> bookmarks = new ArrayList<HistoryItem>();
|
||
10 years ago
|
File bookUrl = new File(context.getFilesDir(), "bookurl");
|
||
10 years ago
|
File book = new File(context.getFilesDir(), "bookmarks");
|
||
|
try {
|
||
|
BufferedReader readUrl = new BufferedReader(new FileReader(bookUrl));
|
||
|
BufferedReader readBook = new BufferedReader(new FileReader(book));
|
||
|
String u, t;
|
||
10 years ago
|
while ((u = readUrl.readLine()) != null && (t = readBook.readLine()) != null) {
|
||
10 years ago
|
HistoryItem map = new HistoryItem(u, t, R.drawable.ic_bookmark);
|
||
|
bookmarks.add(map);
|
||
|
}
|
||
|
readBook.close();
|
||
|
readUrl.close();
|
||
|
} catch (FileNotFoundException ignored) {
|
||
|
} catch (IOException ignored) {
|
||
|
}
|
||
|
return bookmarks;
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static String[] getArray(String input) {
|
||
|
return input.split("\\|\\$\\|SEPARATOR\\|\\$\\|");
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static void trimCache(Context context) {
|
||
|
try {
|
||
|
File dir = context.getCacheDir();
|
||
|
|
||
|
if (dir != null && dir.isDirectory()) {
|
||
|
deleteDir(dir);
|
||
|
}
|
||
|
} catch (Exception ignored) {
|
||
|
|
||
|
}
|
||
|
}
|
||
10 years ago
|
|
||
10 years ago
|
public static boolean deleteDir(File dir) {
|
||
|
if (dir != null && dir.isDirectory()) {
|
||
|
String[] children = dir.list();
|
||
|
for (String aChildren : children) {
|
||
|
boolean success = deleteDir(new File(dir, aChildren));
|
||
|
if (!success) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
// The directory is now empty so delete it
|
||
10 years ago
|
return dir != null && dir.delete();
|
||
10 years ago
|
}
|
||
9 years ago
|
|
||
|
/**
|
||
|
* Creates and returns a new favicon which is the same as the provided
|
||
|
* favicon but with horizontal or vertical padding of 4dp
|
||
|
*
|
||
|
* @param bitmap
|
||
|
* is the bitmap to pad.
|
||
|
* @return the padded bitmap.
|
||
|
*/
|
||
9 years ago
|
public static Bitmap padFavicon(Bitmap bitmap) {
|
||
|
int padding = Utils.convertDpToPixels(4);
|
||
9 years ago
|
|
||
|
Bitmap paddedBitmap = Bitmap.createBitmap(bitmap.getWidth() + padding, bitmap.getHeight()
|
||
|
+ padding, Bitmap.Config.ARGB_8888);
|
||
|
|
||
|
Canvas canvas = new Canvas(paddedBitmap);
|
||
|
canvas.drawARGB(0x00, 0x00, 0x00, 0x00); // this represents white color
|
||
|
canvas.drawBitmap(bitmap, padding / 2, padding / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
|
||
|
|
||
|
return paddedBitmap;
|
||
|
}
|
||
9 years ago
|
|
||
|
@SuppressLint("SimpleDateFormat")
|
||
|
public static File createImageFile() throws IOException {
|
||
|
// Create an image file name
|
||
|
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
|
||
|
String imageFileName = "JPEG_" + timeStamp + "_";
|
||
|
File storageDir = Environment
|
||
|
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||
|
File imageFile = File.createTempFile(imageFileName, /* prefix */
|
||
|
".jpg", /* suffix */
|
||
|
storageDir /* directory */
|
||
|
);
|
||
|
return imageFile;
|
||
|
}
|
||
|
|
||
|
public static Bitmap getWebpageBitmap(Resources resources, boolean dark) {
|
||
|
if (dark) {
|
||
|
if (mWebIconDark == null) {
|
||
|
mWebIconDark = BitmapFactory.decodeResource(resources, R.drawable.ic_webpage_dark);
|
||
|
}
|
||
|
return mWebIconDark;
|
||
|
} else {
|
||
|
if (mWebIconLight == null) {
|
||
|
mWebIconLight = BitmapFactory.decodeResource(resources, R.drawable.ic_webpage);
|
||
|
}
|
||
|
return mWebIconLight;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private static Bitmap mWebIconLight;
|
||
|
private static Bitmap mWebIconDark;
|
||
|
|
||
10 years ago
|
}
|