Java tutorial
package jp.co.rakuten.rakutenvideoplayer.base; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.Enumeration; import java.util.Hashtable; import java.util.List; import jp.co.rakuten.rakutenvideoplayer.R; import jp.co.rakuten.rakutenvideoplayer.alarm.SaveVideoResumeHandle; import jp.co.rakuten.rakutenvideoplayer.api.Api_Play; import jp.co.rakuten.rakutenvideoplayer.api.Api_Resume; import jp.co.rakuten.rakutenvideoplayer.api.Api_ResumeVideo; import jp.co.rakuten.rakutenvideoplayer.api.Api_Trailer; import jp.co.rakuten.rakutenvideoplayer.api.param.PlayParam; import jp.co.rakuten.rakutenvideoplayer.api.param.ResumeParam; import jp.co.rakuten.rakutenvideoplayer.api.param.TrailerParam; import jp.co.rakuten.rakutenvideoplayer.database.DatabaseSQLLiteHelper; import jp.co.rakuten.rakutenvideoplayer.interfaceproject.UpdateData; import jp.co.rakuten.rakutenvideoplayer.model.BaseModel; import jp.co.rakuten.rakutenvideoplayer.model.play.PlayNextContentModel; import jp.co.rakuten.rakutenvideoplayer.model.play.PlayResultModel; import jp.co.rakuten.rakutenvideoplayer.model.play.PlayResumeModel; import jp.co.rakuten.rakutenvideoplayer.model.play.PlaySessionModel; import jp.co.rakuten.rakutenvideoplayer.util.Constant.API; import jp.co.rakuten.rakutenvideoplayer.util.DLog; import org.apache.http.NameValuePair; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.app.ProgressDialog; import android.content.DialogInterface; import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnKeyListener; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; /** * * @author luongcamminh * */ public abstract class BaseActivity extends FragmentActivity implements UpdateData { private Button btnHelp, btnConvention, btnTransaction, btnPersional; private TextView tvInformation, tvLicensecode; private static final String TAG = BaseActivity.class.getSimpleName(); private DatabaseSQLLiteHelper databaseSQLLiteHelper = null; private final String TABLE_VIDEO = "video"; private ProgressDialog progressDialog; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); createDataBase(); progressDialog = new ProgressDialog(this); } /** * Create database */ private void createDataBase() { final String DATA_BASE_NAME = "RakutenVideoDataBase"; final int dataBaseVersion = 1; String createTable = "create table video " + "(video_id text primary key, content_id text, content_name text, url text," + "image_url text,r_enabled text, r_polling_time text, r_position text," + "r_updated_at text, ss_enabled text, ss_token text," + "nc_name text, nc_url text, nc_url_image text)"; String[] table = new String[] { createTable }; String[] tableName = new String[] { TABLE_VIDEO }; databaseSQLLiteHelper = new DatabaseSQLLiteHelper(this, DATA_BASE_NAME, dataBaseVersion, table, tableName); } /** * Check validate of String if string is null or empty return false */ private boolean checkValidateString(String param) { if (param == null || param.equals("")) return false; return true; } private String changeNullStringToValueDefault(String param) { if (param == null) param = ""; return param; } /** * Insert value in table video * * @param playResultModel * @param itemId * @return */ public boolean insertFiledInTableVideo(PlayResultModel playResultModel, String itemId) { if (playResultModel == null) return false; Hashtable<String, String> hashtable = new Hashtable<String, String>(); hashtable.put("video_id", itemId); hashtable.put("content_id", changeNullStringToValueDefault(playResultModel.getContent_id())); hashtable.put("content_name", changeNullStringToValueDefault(playResultModel.getContent_name())); hashtable.put("url", changeNullStringToValueDefault(playResultModel.getUrl())); hashtable.put("image_url", changeNullStringToValueDefault(playResultModel.getImage_url())); if (playResultModel.getResume() != null) { hashtable.put("r_enabled", changeNullStringToValueDefault(playResultModel.getResume().getEnabled())); hashtable.put("r_polling_time", changeNullStringToValueDefault(playResultModel.getResume().getPolling_time())); hashtable.put("r_position", changeNullStringToValueDefault(playResultModel.getResume().getPosition())); hashtable.put("r_updated_at", changeNullStringToValueDefault(playResultModel.getResume().getUpdated_at())); } if (playResultModel.getSingle_session() != null) { hashtable.put("ss_enabled", changeNullStringToValueDefault(playResultModel.getSingle_session().getEnabled())); hashtable.put("ss_token", changeNullStringToValueDefault(playResultModel.getSingle_session().getToken())); } if (playResultModel.getNext_content() != null) { hashtable.put("nc_name", changeNullStringToValueDefault(playResultModel.getNext_content().getName())); hashtable.put("nc_url", changeNullStringToValueDefault(playResultModel.getNext_content().getUrl())); hashtable.put("nc_url_image", changeNullStringToValueDefault(playResultModel.getNext_content().getImage_url())); } String idKey, idValue; idKey = "video_id"; if (checkValidateString(itemId)) idValue = itemId; else idValue = ""; return databaseSQLLiteHelper.insertFiledTable(TABLE_VIDEO, hashtable, idKey, idValue); } /** * Update value table video * * @param playResultModel * @param itemId * @return */ public boolean updateFiledInTableVideo(PlayResultModel playResultModel, String itemId) { if (playResultModel == null || checkValidateString(itemId) == false) return false; Hashtable<String, String> hashtable = new Hashtable<String, String>(); hashtable.put("video_id", itemId); hashtable.put("content_id", changeNullStringToValueDefault(playResultModel.getContent_id())); hashtable.put("content_name", changeNullStringToValueDefault(playResultModel.getContent_name())); hashtable.put("url", changeNullStringToValueDefault(playResultModel.getUrl())); hashtable.put("image_url", changeNullStringToValueDefault(playResultModel.getImage_url())); if (playResultModel.getResume() != null) { hashtable.put("r_enabled", changeNullStringToValueDefault(playResultModel.getResume().getEnabled())); hashtable.put("r_polling_time", changeNullStringToValueDefault(playResultModel.getResume().getPolling_time())); hashtable.put("r_position", changeNullStringToValueDefault(playResultModel.getResume().getPosition())); hashtable.put("r_updated_at", changeNullStringToValueDefault(playResultModel.getResume().getUpdated_at())); } if (playResultModel.getSingle_session() != null) { hashtable.put("ss_enabled", changeNullStringToValueDefault(playResultModel.getSingle_session().getEnabled())); hashtable.put("ss_token", changeNullStringToValueDefault(playResultModel.getSingle_session().getToken())); } if (playResultModel.getNext_content() != null) { hashtable.put("nc_name", changeNullStringToValueDefault(playResultModel.getNext_content().getName())); hashtable.put("nc_url", changeNullStringToValueDefault(playResultModel.getNext_content().getUrl())); hashtable.put("nc_url_image", changeNullStringToValueDefault(playResultModel.getNext_content().getImage_url())); } String idKey, idValue; idKey = "video_id"; if (checkValidateString(itemId)) idValue = itemId; else idValue = ""; return databaseSQLLiteHelper.updateFiledTable(TABLE_VIDEO, hashtable, idKey, idValue); } /** * Delete data video before 2 week * * @return boolean */ public void deleteDataVideoBeforeTwoWeek() { String[] tableVideoKey = new String[] { "video_id", "content_id", "content_name", "url", "image_url", "r_enabled", "r_polling_time", "r_position", "r_updated_at", "ss_enabled", "ss_token", "nc_name", "nc_url", "nc_url_image" }; ArrayList<Hashtable<String, String>> getTable = databaseSQLLiteHelper.getDataFromTable(TABLE_VIDEO, tableVideoKey); String valueItemId = null, valueItemUpdate = null; for (int i = 0; i < getTable.size(); i++) { Enumeration<String> names; names = getTable.get(i).keys(); while (names.hasMoreElements()) { String key = (String) names.nextElement(); DLog.i("DATABASE", key + ": " + getTable.get(i).get(key)); // Update value need if (key.equals("video_id")) valueItemId = getTable.get(i).get(key); if (key.equals("r_updated_at")) valueItemUpdate = getTable.get(i).get(key); } if (checkConditionDetele(valueItemId, valueItemUpdate)) databaseSQLLiteHelper.DeleteFiledTable(TABLE_VIDEO, "video_id", valueItemId); } } /*** * Check time before 2 week * * @param date * @return boolean */ @SuppressLint("SimpleDateFormat") private boolean checkTimeBeforeTwoWeek(String date) { DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss"); String currentDateandTime = formatter.format(new Date()); @SuppressWarnings("deprecation") long diff = Date.parse(currentDateandTime) - Date.parse(date); float dayCount = (float) diff / (24 * 60 * 60 * 1000); if (dayCount >= 14.0) return true; else return false; } /*** * Check condition delete * * @param date * ,id * @return boolean */ private boolean checkConditionDetele(String valueItemId, String valueItemUpdate) { if (checkValidateString(valueItemUpdate)) { if (checkTimeBeforeTwoWeek(valueItemUpdate)) { if (checkValidateString(valueItemId)) return true; } } return false; } /** * Show dialog error * @param errStr */ public void showDialogError(String errStr) { AlertDialog.Builder adb = new AlertDialog.Builder(this); adb.setIcon(R.drawable.icon); adb.setTitle(R.string.str_ErrPlay_Title); adb.setMessage(errStr); adb.setPositiveButton(R.string.str_OK, new OnClickListener() { public void onClick(DialogInterface a0, int a1) { finish(); } }); adb.setOnKeyListener(new OnKeyListener() { @Override public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { dialog.dismiss(); finish(); return true; } return false; } }); AlertDialog ad = adb.create(); ad.show(); } /** * Show dialog error * @param errStr */ public void showDialogInformation(String errStr) { AlertDialog.Builder adb = new AlertDialog.Builder(this); adb.setIcon(R.drawable.icon); adb.setTitle(R.string.str_ErrPlay_Title); adb.setMessage(errStr); adb.setPositiveButton(R.string.str_OK, new OnClickListener() { public void onClick(DialogInterface a0, int a1) { return; } }); AlertDialog ad = adb.create(); ad.show(); } /** * when user click common to Webview. * * @param activity * @param url */ protected void onClickCommon(String url) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); startActivity(i); } protected void initViewCommom(Activity activity, View.OnClickListener clickListener) { btnConvention = (Button) activity.findViewById(R.id.common_left_convention); btnHelp = (Button) activity.findViewById(R.id.common_left_help); btnPersional = (Button) activity.findViewById(R.id.common_left_persional); btnTransaction = (Button) activity.findViewById(R.id.common_left_transaction); tvInformation = (TextView) activity.findViewById(R.id.common_bottom_information); tvLicensecode = (TextView) activity.findViewById(R.id.common_bottom_licensecode); btnConvention.setOnClickListener(clickListener); btnHelp.setOnClickListener(clickListener); btnPersional.setOnClickListener(clickListener); btnTransaction.setOnClickListener(clickListener); tvInformation.setOnClickListener(clickListener); tvLicensecode.setOnClickListener(clickListener); } /** * search data update last * @return */ public Hashtable<String, String> findDayUpdate() { String[] tableVideoKey = new String[] { "video_id", "content_id", "content_name", "url", "image_url", "r_enabled", "r_polling_time", "r_position", "r_updated_at", "ss_enabled", "ss_token", "nc_name", "nc_url", "nc_url_image" }; ArrayList<Hashtable<String, String>> getTable = databaseSQLLiteHelper.getDataFromTable(TABLE_VIDEO, tableVideoKey); int position = 0; if (getTable.size() <= 0) return null; Date dateUpdate = stringToDate(getTable.get(0).get("r_updated_at")); for (int i = 0; i < getTable.size(); i++) { Date dateCheck = stringToDate(getTable.get(i).get("r_updated_at")); if (dateUpdate.compareTo(dateCheck) < 0) { dateUpdate = dateCheck; position = i; } } return getTable.get(position); } public PlayResultModel changeHashTableToPlayResultMode(Hashtable<String, String> hashtable) { PlayResultModel playResultModel = new PlayResultModel(); PlayResumeModel playResumeModel = new PlayResumeModel(); PlayNextContentModel playNextContentModel = new PlayNextContentModel(); PlaySessionModel playSessionModel = new PlaySessionModel(); Enumeration<String> names; names = hashtable.keys(); while (names.hasMoreElements()) { String key = (String) names.nextElement(); DLog.i("DATABASE", key + ": " + hashtable.get(key)); // Update value need if (key.equals("content_id")) playResultModel.setContent_id(hashtable.get(key)); if (key.equals("content_name")) playResultModel.setContent_name(hashtable.get(key)); if (key.equals("url")) playResultModel.setUrl(hashtable.get(key)); if (key.equals("image_url")) playResultModel.setImage_url(hashtable.get(key)); if (key.equals("r_enabled")) playResumeModel.setEnabled(hashtable.get(key)); if (key.equals("r_polling_time")) playResumeModel.setPolling_time(hashtable.get(key)); if (key.equals("r_position")) playResumeModel.setPosition(hashtable.get(key)); if (key.equals("r_updated_at")) playResumeModel.setUpdated_at(hashtable.get(key)); if (key.equals("ss_enabled")) playSessionModel.setEnabled(hashtable.get(key)); if (key.equals("ss_token")) playSessionModel.setToken(hashtable.get(key)); if (key.equals("nc_name")) playNextContentModel.setName(hashtable.get(key)); if (key.equals("nc_url")) playNextContentModel.setUrl(hashtable.get(key)); if (key.equals("nc_url_image")) playNextContentModel.setImage_url(hashtable.get(key)); playResultModel.setResume(playResumeModel); playResultModel.setNext_content(playNextContentModel); playResultModel.setSingle_session(playSessionModel); } return playResultModel; } public String getItemIdVideo(Hashtable<String, String> hashtable) { String item = ""; Enumeration<String> names; names = hashtable.keys(); while (names.hasMoreElements()) { String key = (String) names.nextElement(); DLog.i("DATABASE", key + ": " + hashtable.get(key)); // Update value need if (key.equals("video_id")) item = hashtable.get(key); } return item; } /** * convert string to date * @param dateString * @return */ public Date stringToDate(String dateString) { SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss"); Date convertedDate = new Date(); try { convertedDate = format.parse(dateString); return convertedDate; } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } /** * show progress dialog */ public void showProgressDialog() { if (progressDialog != null) { progressDialog.setMessage("Loading data"); progressDialog.setCancelable(true); progressDialog.show(); } } /** * dismiss progress dialog */ public void dismissProgressDialog() { if (progressDialog != null) { progressDialog.dismiss(); } } /** * send api play */ protected void callAPIGetInformationPlay(String deviceId, String videoId, String attribute, String memberId) { Api_Play api = new Api_Play(this); PlayParam param = new PlayParam(); List<NameValuePair> taskPairs = param.makeTaskPair(deviceId, videoId, attribute, memberId); api.execute(taskPairs); } /** * send api trailer */ protected void callAPIGetInformationTrailer(String deviceId, String videoId) { Api_Trailer api = new Api_Trailer(this); TrailerParam param = new TrailerParam(); List<NameValuePair> taskPairs = param.makeTaskPair(deviceId, videoId); api.execute(taskPairs); } /** * send api resume */ protected void callAPIGetInformationResume(String videoId, String position, String token, String updateAt, String isStart) { Api_Resume api = new Api_Resume(this); ResumeParam param = new ResumeParam(); List<NameValuePair> taskPairs = param.makeTaskPair(videoId, position, token, updateAt, isStart); api.execute(taskPairs); } /** * send api resume video */ protected void callAPIGetInformationResumeVideo() { Api_ResumeVideo api = new Api_ResumeVideo(this); List<NameValuePair> taskPairs = null; api.execute(taskPairs); } }