/*
* Copyright (c) 2015 [1076559197@qq.com | tchen0707@gmail.com]
*
* 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 com.dreamspace.uucampus.common.utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.dreamspace.uucampus.R;
import com.dreamspace.uucampus.model.ErrorRes;
import java.io.File;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Pattern;
import de.hdodenhof.circleimageview.CircleImageView;
import retrofit.RetrofitError;
public class CommonUtils {
/**
* return if str is empty
*
* @param str
* @return
*/
public static boolean isEmpty(String str) {
if (str == null || str.length() == 0 || str.equalsIgnoreCase("null") || str.isEmpty() || str.equals("")) {
return true;
} else {
return false;
}
}
public static ErrorRes getErrorInfo(RetrofitError error){
return (ErrorRes) error.getBodyAs(ErrorRes.class);
}
/**
* get format date
*
* @param timemillis
* @return
*/
public static String getFormatDate(long timemillis) {
return new SimpleDateFormat("yyyy年MM月dd日").format(new Date(timemillis));
}
/**
* decode Unicode string
*
* @param s
* @return
*/
public static String decodeUnicodeStr(String s) {
StringBuilder sb = new StringBuilder(s.length());
char[] chars = s.toCharArray();
for (int i = 0; i < chars.length; i++) {
char c = chars[i];
if (c == '\\' && chars[i + 1] == 'u') {
char cc = 0;
for (int j = 0; j < 4; j++) {
char ch = Character.toLowerCase(chars[i + 2 + j]);
if ('0' <= ch && ch <= '9' || 'a' <= ch && ch <= 'f') {
cc |= (Character.digit(ch, 16) << (3 - j) * 4);
} else {
cc = 0;
break;
}
}
if (cc > 0) {
i += 5;
sb.append(cc);
continue;
}
}
sb.append(c);
}
return sb.toString();
}
/**
* encode Unicode string
*
* @param s
* @return
*/
public static String encodeUnicodeStr(String s) {
StringBuilder sb = new StringBuilder(s.length() * 3);
for (char c : s.toCharArray()) {
if (c < 256) {
sb.append(c);
} else {
sb.append("\\u");
sb.append(Character.forDigit((c >>> 12) & 0xf, 16));
sb.append(Character.forDigit((c >>> 8) & 0xf, 16));
sb.append(Character.forDigit((c >>> 4) & 0xf, 16));
sb.append(Character.forDigit((c) & 0xf, 16));
}
}
return sb.toString();
}
/**
* convert time str
*
* @param time
* @return
*/
public static String convertTime(int time) {
time /= 1000;
int minute = time / 60;
int second = time % 60;
minute %= 60;
return String.format("%02d:%02d", minute, second);
}
/**
* url is usable
*
* @param url
* @return
*/
public static boolean isUrlUsable(String url) {
if (CommonUtils.isEmpty(url)) {
return false;
}
URL urlTemp = null;
HttpURLConnection connt = null;
try {
urlTemp = new URL(url);
connt = (HttpURLConnection) urlTemp.openConnection();
connt.setRequestMethod("HEAD");
int returnCode = connt.getResponseCode();
if (returnCode == HttpURLConnection.HTTP_OK) {
return true;
}
} catch (Exception e) {
return false;
} finally {
connt.disconnect();
}
return false;
}
/**
* is url
*
* @param url
* @return
*/
public static boolean isUrl(String url) {
Pattern pattern = Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
return pattern.matcher(url).matches();
}
/**
* get toolbar height
*
* @param context
* @return
*/
public static int getToolbarHeight(Context context) {
final TypedArray styledAttributes = context.getTheme().obtainStyledAttributes(
new int[]{android.R.attr.actionBarSize});
int toolbarHeight = (int) styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
return toolbarHeight;
}
//使用Glide加载网络图片
public static void showImageWithGlide(Context context, final CircleImageView imageView,String url){
Glide.with(context)
.load(url)
.placeholder(new ColorDrawable(context.getResources().getColor(R.color.no_image_color)))
.centerCrop()
.into(new SimpleTarget<GlideDrawable>() {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
imageView.setImageDrawable(resource);
}
});
}
public static void showImageWithGlide(Context context,ImageView imageView,String url){
Glide.with(context)
.load(url)
.placeholder(new ColorDrawable(context.getResources().getColor(R.color.no_image_color)))
.centerCrop()
.into(imageView);
}
public static void showImageWithGlideInCiv(Context context,CircleImageView circleImageView,String url){
Glide.with(context)
.load(url)
.error(new ColorDrawable(context.getResources().getColor(R.color.no_image_color)))
.centerCrop()
.into(circleImageView);
}
//控制editText输入的小数位数
public static void setPricePoint(final EditText editText) {
editText.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
if (s.toString().contains(".")) {
if (s.length() - 1 - s.toString().indexOf(".") > 2) {
s = s.toString().subSequence(0,
s.toString().indexOf(".") + 3);
editText.setText(s);
editText.setSelection(s.length());
}
}
if (s.toString().trim().substring(0).equals(".")) {
s = "0" + s;
editText.setText(s);
editText.setSelection(2);
}
if (s.toString().startsWith("0")
&& s.toString().trim().length() > 1) {
if (!s.toString().substring(1, 2).equals(".")) {
editText.setText(s.subSequence(0, 1));
editText.setSelection(1);
return;
}
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
public static String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
}