个人封装爱好,不对之处,欢迎指正~
BaseActivity.java
import android.Manifest;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.ColorRes;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.StringRes;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.zzgx.ipcamera.R;
import com.zzgx.ipcamera.config.AppConfig;
public class BaseActivity extends AppCompatActivity {
public TextView btnToolBarLeft;
public TextView tvToolBarTitle;
public TextView btnToolBarRight;
public TextView btnToolBarRight2;
public RelativeLayout lyToolBar;
public Toolbar toolbar;
private static Context context;
View.OnClickListener mClickListener;
private Toast toast;
private OnToolBarItemClickListener toolBarItemClickListener;
private ProgressDialog progressDialog;
private boolean activing = false;
private Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what == 0x4444)
hideProgress();
return false;
}
});
@Override
protected void onCreate(Bundle savedInstanceState) {
context = this;
super.onCreate(savedInstanceState);
requestPermissions();
}
/**
* 初始化视图
*/
protected void initView() {
initToolBar();
hideToolBarItem();
}
/**
* 初始化ToolBar
*/
protected void initToolBar() {
btnToolBarLeft = (TextView) findViewById(R.id.btn_toolBar_left);
btnToolBarRight = (TextView) findViewById(R.id.btn_toolBar_right);
btnToolBarRight2 = (TextView) findViewById(R.id.btn_toolBar_right2);
tvToolBarTitle = (TextView) findViewById(R.id.tv_toolBar_title);
lyToolBar = (RelativeLayout) findViewById(R.id.ly_toolBar);
toolbar = (Toolbar) findViewById(R.id.toolBar);
if (null != btnToolBarLeft)
btnToolBarLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onToolBarLeftItemClick(v);
if (null != toolBarItemClickListener)
toolBarItemClickListener.onToolBarLeftItemClick(v);
}
});
if (null != btnToolBarRight)
btnToolBarRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onToolBarRightItemClick(v);
if (null != toolBarItemClickListener)
toolBarItemClickListener.onToolBarRightItemClick(v);
}
});
if (null != btnToolBarRight2)
btnToolBarRight2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onToolBarRight2ItemClick(v);
if (null != toolBarItemClickListener)
toolBarItemClickListener.onToolBarRight2ItemClick(v);
}
});
}
/**
* 初始化注册
*/
protected void initRegister() {
if (null == mClickListener) initClickListener();
}
private void initClickListener() {
mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
onViewClick(v);
}
};
}
/**
* 视图事件点击
*
* @param view
*/
protected void onViewClick(View view) {
}
/**
* 标题栏左导航按钮点击
*
* @param view
*/
protected void onToolBarLeftItemClick(View view) {
}
/**
* 标题栏右导航按钮点击
*
* @param view
*/
protected void onToolBarRightItemClick(View view) {
}
/**
* 标题栏右导航第二个按钮点击
*
* @param view
*/
protected void onToolBarRight2ItemClick(View view) {
}
/**
* 隐藏ToolBar
*/
protected void hideToolBar() {
if (null != lyToolBar) lyToolBar.setVisibility(View.GONE);
}
/**
* 隐藏ToolBar
*/
protected void showToolBar() {
if (null != lyToolBar) lyToolBar.setVisibility(View.VISIBLE);
}
/**
* 显示标题栏导航按钮
*/
protected void showToolBarItem() {
if (null != btnToolBarLeft) btnToolBarLeft.setVisibility(View.VISIBLE);
if (null != btnToolBarRight) btnToolBarRight.setVisibility(View.VISIBLE);
if (null != btnToolBarRight2) btnToolBarRight2.setVisibility(View.VISIBLE);
}
/**
* 隐藏标题栏导航按钮
*/
protected void hideToolBarItem() {
if (null != btnToolBarLeft) btnToolBarLeft.setVisibility(View.GONE);
if (null != btnToolBarRight) btnToolBarRight.setVisibility(View.GONE);
if (null != btnToolBarRight2) btnToolBarRight2.setVisibility(View.GONE);
}
/**
* 显示标题栏左导航按钮
*/
protected void showToolBarLeftItem() {
if (null != btnToolBarLeft) btnToolBarLeft.setVisibility(View.VISIBLE);
}
/**
* 隐藏标题栏左导航按钮
*/
protected void hideToolBarLeftItem() {
if (null != btnToolBarLeft) btnToolBarLeft.setVisibility(View.GONE);
}
/**
* 显示标题栏右导航按钮
*/
protected void showToolBarRightItem() {
if (null != btnToolBarRight) btnToolBarRight.setVisibility(View.VISIBLE);
}
/**
* 隐藏标题栏右导航按钮
*/
protected void hideToolBarRightItem() {
if (null != btnToolBarRight) btnToolBarRight.setVisibility(View.GONE);
}
/**
* 显示标题栏右导航第二个按钮
*/
protected void showToolBarRight2Item() {
if (null != btnToolBarRight2) btnToolBarRight2.setVisibility(View.VISIBLE);
}
/**
* 隐藏标题栏右导航第二个按钮
*/
protected void hideToolBarRight2Item() {
if (null != btnToolBarRight2) btnToolBarRight2.setVisibility(View.GONE);
}
/**
* set the toolBar background color
*
* @param resId
*/
public void setToolBarBg(@ColorRes int resId) {
if (null != toolbar) toolbar.setBackgroundColor(getResources().getColor(resId));
}
/**
* 设置标题栏左导航按钮图标
*
* @param resId 资源Id
*/
protected void setToolBarLeftItemIco(@DrawableRes int resId) {
showToolBarLeftItem();
if (null != btnToolBarLeft) {
Drawable drawable = getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
btnToolBarLeft.setCompoundDrawables(drawable, null, null, null);
btnToolBarLeft.setText("");
}
}
/**
* 设置标题栏右导航按钮图标
*
* @param resId 资源Id
*/
protected void setToolBarRightItemIco(@DrawableRes int resId) {
showToolBarRightItem();
if (null != btnToolBarRight) {
Drawable drawable = getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
btnToolBarRight.setCompoundDrawables(null, null, drawable, null);
btnToolBarRight.setText("");
}
}
/**
* 设置标题栏右导航按钮图标
*
* @param resId 资源Id
*/
protected void setToolBarRight2ItemIco(@DrawableRes int resId) {
showToolBarRight2Item();
if (null != btnToolBarRight2) {
Drawable drawable = getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
btnToolBarRight2.setCompoundDrawables(null, null, drawable, null);
btnToolBarRight2.setText("");
}
}
/**
* 设置标题栏左导航按钮图标和文字
*
* @param resId 资源Id
* @param text 文本
*/
protected void setToolBarLeftItem(@DrawableRes int resId, String text) {
showToolBarLeftItem();
if (null != btnToolBarLeft) {
Drawable drawable = null;
if (resId > 0) {
drawable = getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
}
btnToolBarLeft.setCompoundDrawables(drawable, null, null, null);
btnToolBarLeft.setText(text);
}
}
/**
* 设置标题栏右导航按钮图标和文字
*
* @param resId 资源Id
* @param text 文本
*/
protected void setToolBarRightItem(@DrawableRes int resId, String text) {
showToolBarRightItem();
if (null != btnToolBarRight) {
Drawable drawable = null;
if (resId > 0) {
drawable = getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
}
btnToolBarRight.setCompoundDrawables(null, null, drawable, null);
btnToolBarRight.setText(text);
}
}
/**
* 设置标题栏右导航按钮图标和文字
*
* @param resId 资源Id
* @param text 文本
*/
protected void setToolBarRight2Item(@DrawableRes int resId, String text) {
showToolBarRight2Item();
if (null != btnToolBarRight2) {
Drawable drawable = null;
if (resId > 0) {
drawable = getResources().getDrawable(resId);
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
}
btnToolBarRight2.setCompoundDrawables(null, null, drawable, null);
btnToolBarRight2.setText(text);
}
}
/**
* 设置标题
*
* @param text
*/
protected void setToolBarTitle(String text) {
if (null != tvToolBarTitle) tvToolBarTitle.setText(text);
}
/**
* 设置标题
*
* @param resId
*/
protected void setToolBarTitle(@StringRes int resId) {
if (null != tvToolBarTitle) tvToolBarTitle.setText(resId);
}
@Override
public View findViewById(@IdRes int id) {
return super.findViewById(id);
}
/**
* 设置点击监听器
*
* @param view
*/
public void setClickListener(View view) {
if (null == mClickListener) initClickListener();
if (null != view)
view.setOnClickListener(mClickListener);
else throw new NullPointerException("This view can't be null");
}
/**
* show the progress dialog
*
* the dialog can cancle when touched outside the window
* and with the{@link KeyEvent#KEYCODE_BACK BACK} key
*/
public void showProgress() {
showProgress(getString(R.string.loading), false, true);
}
/**
* show the progress dialog
*
*
* @param cancleable this dialog is cancelable with the {@link KeyEvent#KEYCODE_BACK BACK} key.
*/
public void showProgress(boolean cancleable) {
showProgress(getString(R.string.loading), false, cancleable);
}
/**
* show the progress dialog
*
* the dialog can cancle when touched outside the window
* and with the{@link KeyEvent#KEYCODE_BACK BACK} key
*
* @param msg
*/
public void showProgress(String msg) {
showProgress(msg, false, true);
}
/**
* show the progress dialog
*
* the dialog can cancle when touched outside the window
* and with the{@link KeyEvent#KEYCODE_BACK BACK} key
*
* @param resId
*/
public void showProgress(@StringRes int resId) {
showProgress(getResources().getString(resId), false, true);
}
/**
* show the progress dialog
*
*
* @param msg
* @param cancleable this dialog is cancelable with the {@link KeyEvent#KEYCODE_BACK BACK} key.
*/
public void showProgress(String msg, boolean cancleable) {
showProgress(msg, false, cancleable);
}
/**
* show the progress dialog
*
*
* @param resId
* @param cancleable this dialog is cancelable with the {@link KeyEvent#KEYCODE_BACK BACK} key.
*/
public void showProgress(@StringRes int resId, boolean cancleable) {
showProgress(getResources().getString(resId), false, cancleable);
}
/**
* show the progress dialog
*
* @param msg msg
* @param touchOutsideCancleable the dialog should be canceled when touched outside the window.
* @param isCancelable this dialog is cancelable with the {@link KeyEvent#KEYCODE_BACK BACK} key.
*/
public void showProgress(String msg, boolean touchOutsideCancleable, boolean isCancelable) {
if (!activing) return;
if (null == progressDialog)
progressDialog = new ProgressDialog(this);
progressDialog.setCustomTitle(null);
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progressDialog.setMessage(msg);
progressDialog.setCancelable(isCancelable);
try {
handler.sendEmptyMessageDelayed(0x4444, 50000);
progressDialog.show();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
/**
* hide the progress dialog
*/
public void hideProgress() {
if (null != progressDialog && progressDialog.isShowing()) {
try {
progressDialog.cancel();
} catch (Exception e) {
} finally {
}
}
}
/**
* 显示长时间Toast
*
* @param msg
*/
public void showLongToast(String msg) {
showToast(msg, Toast.LENGTH_LONG);
}
/**
* 显示长时间Toast
*
* @param resId
*/
public void showLongToast(@StringRes int resId) {
showLongToast(getResources().getString(resId));
}
/**
* 显示长时间Toast
*
* @param msg
*/
public void showLongToast(String msg, int code) {
showToast(AppConfig.DEBUG ? "Code: " + code + " " + msg : msg, Toast.LENGTH_LONG);
}
/**
* 显示Toast
*
* @param msg
*/
public void showToast(String msg) {
showToast(msg, Toast.LENGTH_SHORT);
}
/**
* 显示Toast
*
* @param msg
*/
public void showToast(int code, String msg) {
showToast(AppConfig.DEBUG ? "Code: " + code + " " + msg : msg, Toast.LENGTH_SHORT);
}
/**
* 显示Toast
*
* @param resId
*/
public void showToast(@StringRes int resId) {
showToast(getResources().getString(resId));
}
/**
* 显示Toast
*
* @param resId
*/
public void showToast(@StringRes int resId, int code) {
showToast(code, getResources().getString(resId));
}
/**
* 隐藏Toast
*/
public void hideToast() {
if (null != toast) toast.cancel();
}
private void showToast(String msg, int duration) {
if (null == toast) {
toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM, 0, 100);
}
toast.setDuration(duration);
TextView textView = new TextView(this);
textView.setText(msg);
textView.setTextSize(13);
textView.setTextColor(getResources().getColor(R.color.white));
textView.setBackground(getResources().getDrawable(R.drawable.black_shadow_bg));
toast.setView(textView);
toast.show();
}
/**
* Request the system permissions.
*/
public void requestPermissions() {
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},//需要请求的所有权限,需要请求多个请添加对应权限
0//请求码
);
}
}
/**
* Get BaseActivity context.
*
* @return context
*/
public Context getContext() {
if (null == context)
context = this;
return context;
}
@Override
protected void onResume() {
super.onResume();
activing = true;
}
@Override
protected void onStop() {
super.onStop();
activing = false;
}
@Override
protected void onDestroy() {
activing = false;
super.onDestroy();
}
/**
* Set toolBar navigation button on click listener.
*
* @param listener listener
*/
public void setToolBarItemClickListener(OnToolBarItemClickListener listener) {
this.toolBarItemClickListener = listener;
}
}
layout_toolbar.xml
BaseFragment.java
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* @author Created by ql on 2016/11/4.
*/
public class BaseFragment extends Fragment implements OnToolBarItemClickListener {
private Context context;
private static BaseFragment instance;
private BaseActivity baseActivity;
View.OnClickListener mClickListener;
private static final String STATE_SAVE_OR_HIDDEN = "STATE_SAVE_OR_HIDDEN";
public BaseFragment() {
instance = this;
}
public static BaseFragment newInstance() {
synchronized (BaseFragment.class) {
if (null == instance)
new BaseFragment();
}
return instance;
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
return view;
}
protected void initView() {
}
protected void initRegister() {
if (null == mClickListener) initClickListener();
}
private void initClickListener() {
mClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
onViewClick(v);
}
};
}
protected void onViewClick(View view) {
}
private void init(Bundle savedInstanceState) {
getBaseActivity().setToolBarItemClickListener(this);
//处理内存重启导致的多个Fragment重叠问题
if (savedInstanceState != null) {
boolean isSupportHidden = savedInstanceState.getBoolean(STATE_SAVE_OR_HIDDEN);
FragmentTransaction ft = getFragmentManager().beginTransaction();
if (isSupportHidden) {
ft.hide(this);
} else {
ft.show(this);
}
ft.commit();
}
}
@Override
public Context getContext() {
if (null == context) context = getActivity();
return context;
}
/**
* Get base Activity
*
* @return
*/
public BaseActivity getBaseActivity() {
if (null == baseActivity) baseActivity = (BaseActivity) getActivity();
return baseActivity;
}
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(STATE_SAVE_OR_HIDDEN, isHidden());
super.onSaveInstanceState(outState);
}
public void reloadData() {
}
public boolean onBackPressed() {
return false;
}
public void currFocused(int index) {
}
/**
* 显示长时间Toast
*
* @param msg
*/
public void showLongToast(String msg) {
getBaseActivity().showLongToast(msg);
}
/**
* 显示长时间Toast
*
* @param msg
*/
public void showLongToast(String msg, int code) {
getBaseActivity().showLongToast(msg, code);
}
/**
* 显示长时间Toast
*
* @param resId
*/
public void showLongToast(@StringRes int resId) {
getBaseActivity().showLongToast(resId);
}
/**
* 显示Toast
*
* @param msg
*/
public void showToast(String msg) {
getBaseActivity().showToast(msg);
}
/**
* 显示Toast
*
* @param msg
*/
public void showToast(int code, String msg) {
getBaseActivity().showToast(code, msg);
}
/**
* 显示Toast
*
* @param resId
*/
public void showToast(@StringRes int resId) {
getBaseActivity().showToast(resId);
}
/**
* 显示Toast
*
* @param resId
*/
public void showToast(@StringRes int resId, int code) {
showToast(code, getResources().getString(resId));
}
/**
* show the progress dialog
*
* the dialog can cancle when touched outside the window
* and with the{@link KeyEvent#KEYCODE_BACK BACK} key
*
* @param msg
*/
public void showProgress(String msg) {
getBaseActivity().showProgress(msg, false, true);
}
/**
* show the progress dialog
*
* @param msg msg
* @param touchOutsideCancleable the dialog should be canceled when touched outside the window.
* @param cancleable this dialog is cancelable with the {@link KeyEvent#KEYCODE_BACK BACK} key.
*/
public void showProgress(String msg, boolean touchOutsideCancleable, boolean cancleable) {
getBaseActivity().showProgress(msg, touchOutsideCancleable, cancleable);
}
/**
* hide the progress dialog
*/
public void hideProgress() {
getBaseActivity().hideProgress();
}
/**
* 设置标题栏左导航按钮图标
*
* @param resId 资源Id
*/
protected void setToolBarLeftItemIco(@DrawableRes int resId) {
getBaseActivity().setToolBarLeftItemIco(resId);
}
/**
* 设置标题栏右导航按钮图标
*
* @param resId 资源Id
*/
protected void setToolBarRightItemIco(@DrawableRes int resId) {
getBaseActivity().setToolBarRightItemIco(resId);
}
/**
* 设置标题栏左导航按钮图标和文字
*
* @param resId 资源Id
* @param text 文本
*/
protected void setToolBarLeftItem(@DrawableRes int resId, String text) {
getBaseActivity().setToolBarLeftItem(resId, text);
}
/**
* 设置标题栏右导航按钮图标和文字
*
* @param resId 资源Id
* @param text 文本
*/
protected void setToolBarRightItem(@DrawableRes int resId, String text) {
getBaseActivity().setToolBarRightItem(resId, text);
}
/**
* 设置标题
*
* @param text
*/
protected void setToolBarTitle(String text) {
getBaseActivity().setToolBarTitle(text);
}
/**
* 设置标题
*
* @param resId
*/
protected void setToolBarTitle(@StringRes int resId) {
getBaseActivity().setToolBarTitle(resId);
}
@Override
public boolean onToolBarLeftItemClick(View view) {
return false;
}
@Override
public boolean onToolBarRightItemClick(View view) {
return false;
}
@Override
public boolean onToolBarRight2ItemClick(View view) {
return false;
}
/**
* 设置点击监听器
*
* @param view
*/
public void setClickListener(View view) {
if (null == mClickListener) initClickListener();
if (null != view)
view.setOnClickListener(mClickListener);
}
}
OnToolBarItemClickListener.java
import android.view.View;
/**
* ToolBar navigation button click listener
*
* @author Created by ql on 2016/11/4.
*/
public interface OnToolBarItemClickListener {
/**
* ToolBar left navigation button click
*
* @param view The com.zzgx.libs.view on click
* @return Returns true to indicate that the current fragment is consumed
*/
boolean onToolBarLeftItemClick(View view);
/**
* ToolBar right navigation button click
*
* @param view The com.zzgx.libs.view on click
* @return Returns true to indicate that the current fragment is consumed
*/
boolean onToolBarRightItemClick(View view);
/**
* ToolBar second right navigation button click
*
* @param view The view on click
* @return Returns true to indicate that the current fragment is consumed
*/
boolean onToolBarRight2ItemClick(View view);
}
Log类封装:(带打印位置路径和行号功能)
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import com.zzgx.ipcamera.config.AppConfig;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Lg {
static Context context;
private static String customTagPrefix = "";
static final boolean ENABLE = AppConfig.DEBUG;
static final String TAG = "Lg";
private static SimpleDateFormat format;
public Lg(Context context) {
if (null == context) {
throw new NullPointerException("context can no be null,can be use init() in application");
}
Lg.context = context;
format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.sss");
}
public static void init(Context context) {
new Lg(context);
}
private static String generateTag() {
String tag = "%s.%s(line:%d)";
StackTraceElement caller = Thread.currentThread().getStackTrace()[4];
String callerClazzName = caller.getClassName();
callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ":" + tag;
return "QL: " + tag;
}
private static String generateTag(String _tag) {
String tag = "%s.%s(line:%d)";
StackTraceElement caller = Thread.currentThread().getStackTrace()[4];
String callerClazzName = caller.getClassName();
callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
tag = TextUtils.isEmpty(customTagPrefix) ? tag : customTagPrefix + ": " + tag;
return _tag + ": " + tag;
}
public static void d(String tag, String msg) {
if (!ENABLE) return;
if (msg == null) return;
Log.d(generateTag(tag), msg);
}
public static void d(String tag, boolean logFlag, String msg) {
if (!ENABLE) return;
if (!logFlag) return;
if (msg == null) return;
Log.d(generateTag(tag), msg);
}
public static void v(String tag, String msg) {
if (!ENABLE) return;
if (msg == null) return;
Log.v(generateTag(tag), msg);
}
public static void v(String tag, boolean logFlag, String msg) {
if (!ENABLE) return;
if (!logFlag) return;
if (msg == null) return;
Log.v(generateTag(tag), msg);
}
public static void w(String tag, String msg) {
if (!ENABLE) return;
Log.w(generateTag(tag), msg);
}
public static void w(String tag, boolean logFlag, String msg) {
if (!ENABLE) return;
if (!logFlag) return;
if (msg == null) return;
Log.w(generateTag(tag), msg);
}
public static void i(String tag, String msg) {
if (!ENABLE) return;
Log.i(generateTag(tag), msg);
// if (AppConfig.DEBUG)
// save(tag, "", "", msg);
}
public static void i(String tag, int code, String msg) {
if (!ENABLE) return;
i(generateTag(tag), "Code: " + code + " " + msg);
}
public static void e(String tag, int code, String msg) {
if (!ENABLE) return;
e(generateTag(tag), "Code: " + code + " " + msg);
}
public static void e(String tag, String msg) {
if (!ENABLE) return;
Log.e(generateTag(tag), msg);
// if (AppConfig.DEBUG)
// save(tag, "", "", msg);
}
public static void i(String tag, String msg, byte[] b) {
if (b == null) {
return;
}
Log.i(generateTag(tag), merge(msg, b));
}
public static void e(String tag, String msg, byte[] b) {
if (b == null) {
return;
}
Log.e(generateTag(tag), merge(msg, b));
}
private static String merge(String msg, byte[] b) {
if (TextUtils.isEmpty(msg)) {
msg = "";
}
int length = b.length;
byte[] d = null;
if (length > 200) {
d = new byte[200];
msg += "\"byte is too large, only show 200 lenght.\"";
System.arraycopy(b, 0, d, 0, d.length);
b = d;
}
msg += String.format(" len: %d, bytes: ", length);
for (byte aB : b) {
msg += String.format("%02X ", aB);
}
return msg;
}
public static void e(String tag, boolean logFlag, String msg) {
if (!ENABLE) return;
if (!logFlag) return;
if (msg == null) return;
Log.e(generateTag(tag), msg);
}
public static void l(String tag, String msg) {
if (!ENABLE) return;
System.out.println("TAG:" + tag + "/n" + msg);
}
public static void l(String tag, boolean logFlag, String msg) {
if (!ENABLE) return;
if (!logFlag) return;
if (msg == null) return;
System.out.println("TAG:" + tag + "/n" + msg);
}
public static void save(final String classname, final String page, final String actioname, final String notes) {
if (context == null) {
return;
}
ZZGXThreadPool.execute(new Runnable() {
@Override
public void run() {
File file = null;
File logFile = null;
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "ZZGX");
} else {
file = new File(Environment.getDownloadCacheDirectory().getAbsolutePath() + File.separator + "ZZGX");
}
if (!file.exists()) {
file.mkdirs();
}
logFile = new File(file.getAbsolutePath() + File.separator + "zzgx_log.txt");
if (!logFile.exists()) {
logFile.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
if (logFile == null) {
return;
}
String timer = format.format(new Date(System.currentTimeMillis()));
StringBuilder builder = new StringBuilder();
builder.append(timer)
.append(" ")
.append("")
.append(" ")
.append("")
.append(" ")
.append(classname)
.append(" ")
.append(page)
.append(" ")
.append(actioname)
.append(" ")
.append(notes);
FileOutputStream fos = null;
OutputStreamWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fos = new FileOutputStream(logFile.getAbsolutePath(), true);
fileWriter = new OutputStreamWriter(fos);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(builder.toString());
bufferedWriter.write("\r\n");
bufferedWriter.newLine();
bufferedWriter.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
}