diff --git a/help/HelpActivity.java b/help/HelpActivity.java new file mode 100644 index 0000000..5712b9e --- /dev/null +++ b/help/HelpActivity.java @@ -0,0 +1,382 @@ +package com.sas.android.bimobile.ui.help; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +import android.annotation.SuppressLint; +import android.app.ActionBar; +import android.app.Activity; +import android.graphics.Color; +import android.os.Bundle; +import android.util.DisplayMetrics; +import android.view.Menu; +import android.view.MenuItem; +import android.view.MotionEvent; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.View.OnTouchListener; +import android.view.ViewGroup; +import android.view.Window; +import android.view.WindowManager; +import android.view.WindowManager.LayoutParams; +import android.view.inputmethod.InputMethodManager; +import android.webkit.WebView; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ListView; +import android.widget.SearchView; +import android.widget.SearchView.OnCloseListener; +import android.widget.SearchView.OnQueryTextListener; + +import com.sas.android.bimobile.BIMobile; +import com.sas.android.bimobile.R; +import com.sas.android.bimobile.ui.share.HtmlMailTemplateParser; + +public class HelpActivity extends Activity { + + private static String IS_WEBVIEW_SHOWN = "webView"; + private static String IS_LISTVIEW_SHOWN = "listView"; + private static String WEBVIEW_URL = "web_url"; + private static String TITLE = "title"; + private static String SEARCH_KEYWORD = "keyword"; + private static String IS_CONTENTMENU_SHOWN = "contentMenu"; + private static String ORDER_OF_TOPIC = "orderOfTopic"; + private static String HELP_TOPICS_JSON = "helpTopics.json"; + private String keyword; + private boolean mContent_is_shown = false; + // private Stack> backStack; + private ArrayList orderOfTopic; + + private WebView mWebView; + private MenuItem mContentsMenu; + // private TextView mNoResultHint; + private ListView mTopicListView; + private ArrayList mTopicList; + private SearchView mSearchView; + + private HelpTopicListAdapter mAdapter; + private HelpDocsManager helpDocsManager; + + private void generateTopicList() { + mTopicList = new ArrayList(); + try { + InputStream stream = this.getResources().getAssets().open( + getString(R.string.help_path) + HELP_TOPICS_JSON); + String jsonTopic = HtmlMailTemplateParser.inputStream2String(stream); + helpDocsManager = new HelpDocsManager(this); + helpDocsManager.parseHelpTopics(jsonTopic); + } catch (IOException e) { + e.printStackTrace(); + } + setTopicList(helpDocsManager.get1stLevelTopics()); + } + + private void initWindow() { + requestWindowFeature(Window.FEATURE_ACTION_BAR); + ActionBar actionBar = this.getActionBar(); + actionBar.setDisplayHomeAsUpEnabled(true); + getWindow().setFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND, + WindowManager.LayoutParams.FLAG_DIM_BEHIND); + getWindow().setSoftInputMode(BIND_ADJUST_WITH_ACTIVITY); + setFinishOnTouchOutside(false); + + orderOfTopic = new ArrayList(); + + DisplayMetrics displayMetrics = getResources().getDisplayMetrics(); + int width = displayMetrics.widthPixels * 3 / 4; //? + int height = displayMetrics.heightPixels * 9 / 10; + if (displayMetrics.heightPixels > displayMetrics.widthPixels) { + width = displayMetrics.widthPixels * 9 / 10; + height = displayMetrics.heightPixels * 3 / 4; + } + LayoutParams params = this.getWindow().getAttributes(); + params.alpha = 1.0f; + params.dimAmount = 0.5f; + getWindow().setAttributes(params); + getWindow().setLayout(width, height); + } + + @SuppressLint("SetJavaScriptEnabled") + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + initWindow(); + + setContentView(R.layout.help_fragment); + + setTitle(getString(R.string.help_title)); + + generateTopicList(); + + setupUI(findViewById(R.id.help_fragment)); + + // mNoResultHint = (TextView) findViewById(R.id.no_results); + mTopicListView = (ListView) findViewById(R.id.help_title_list); + mAdapter = new HelpTopicListAdapter(this, mTopicList); + mTopicListView.setAdapter(mAdapter); + mTopicListView.setTextFilterEnabled(true); + mTopicListView.setOnItemClickListener(new OnItemClickListener() { + + @Override + public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) { + // mSearchView.onActionViewCollapsed(); + mSearchView.clearFocus(); + + if (mTopicList.get(arg2).getTitle().equals( + helpDocsManager.get1stLevelTopics().get(0).getTitle())) { + BIMobile.openSplash(HelpActivity.this); + } else if (mTopicList.get(arg2).getSubTopics() == null) { + String url = getString(R.string.help_url) + mTopicList.get(arg2).getHtml() + + ".html"; + showWebView(); + mWebView.loadUrl(url); + mContentsMenu.setVisible(true); + setTitle(mTopicList.get(arg2).getTitle()); + } else { + // if (keyword != null) + orderOfTopic.add(arg2); + mContentsMenu.setVisible(true); + setTitle(mTopicList.get(arg2).getTitle()); + setTopicList(mTopicList.get(arg2).getSubTopics()); + mAdapter.notifyDataSetChanged(); + } + } + }); + + // Load the web view content + mWebView = (WebView) findViewById(R.id.web_view); + mWebView.getSettings().setJavaScriptEnabled(true); + mWebView.setBackgroundColor(Color.TRANSPARENT); // prevent flash of white + + } + + private void setTopicList(ArrayList newTopics) { + mTopicList.clear(); + mTopicList.addAll(newTopics); + } + + private void showTopicList() { + mTopicListView.setVisibility(View.VISIBLE); + mWebView.setVisibility(View.GONE); + } + + private void showWebView() { + mWebView.setVisibility(View.VISIBLE); + mTopicListView.setVisibility(View.GONE); + } + + private void showNoResult() { + mWebView.setVisibility(View.GONE); + mTopicListView.setVisibility(View.GONE); + } + + private void setListTitle(HelpTopic topic) { + if (helpDocsManager.is1stLevelTopic(mTopicList.get(0))) + setTitle(getString(R.string.help_title)); + else + setTitle(topic.getParentTopic().getTitle()); + } + + private void showContentMenu(HelpTopic topic) { + if (helpDocsManager.is1stLevelTopic(topic)) + mContentsMenu.setVisible(false); + else + mContentsMenu.setVisible(true); + } + + @Override + public boolean onCreateOptionsMenu(Menu menu) { + getMenuInflater().inflate(R.menu.help_menu, menu); + mContentsMenu = menu.findItem(R.id.help_content); + if (mContent_is_shown) + mContentsMenu.setVisible(true); + else + mContentsMenu.setVisible(false); + mSearchView = (SearchView) menu.findItem(R.id.help_menu_search).getActionView(); + mSearchView.setIconified(true); + mSearchView.setSubmitButtonEnabled(false); + mSearchView.setQueryHint(getResources().getString(R.string.search_action)); + if (keyword != null && !keyword.equals("")) { + mSearchView.setIconified(false); + mSearchView.setQuery(keyword, true); + } + mSearchView.setOnQueryTextListener(new OnQueryTextListener() { + + @Override + public boolean onQueryTextSubmit(String query) { + return true; + } + + @Override + public boolean onQueryTextChange(String newText) { + orderOfTopic.clear(); + ArrayList searchResult = helpDocsManager.searchHelp(newText); + keyword = newText; + if (searchResult.size() == 0) { + showNoResult(); + } else { + setTopicList(searchResult); + mAdapter.notifyDataSetChanged(); + showTopicList(); + } + return true; + } + }); + + mSearchView.setOnSearchClickListener(new OnClickListener() { + + @Override + public void onClick(View v) { + showTopicList(); + } + }); + + mSearchView.setOnCloseListener(new OnCloseListener() { + + @Override + public boolean onClose() { + setListTitle(mTopicList.get(0)); + if (!mWebView.isShown()) + showContentMenu(mTopicList.get(0)); + return false; + } + }); + return true; + } + + @Override + @SuppressLint("CommitTransaction") + public boolean onOptionsItemSelected(MenuItem item) { + switch (item.getItemId()) { + case android.R.id.home: + goBack(); + return true; + case R.id.help_content: + goHome(); + return true; + default: + return super.onOptionsItemSelected(item); + } + } + + @Override + protected void onRestoreInstanceState(Bundle savedInstanceState) { + setTitle(savedInstanceState.getString(TITLE)); + orderOfTopic = savedInstanceState.getIntegerArrayList(ORDER_OF_TOPIC); + keyword = savedInstanceState.getString(SEARCH_KEYWORD); + if (savedInstanceState.getBoolean(IS_CONTENTMENU_SHOWN)) + mContent_is_shown = true; + else + mContent_is_shown = false; + if (savedInstanceState.getBoolean(IS_LISTVIEW_SHOWN)) { + if (keyword != null && orderOfTopic != null) { + setTopicList(helpDocsManager.findTopicList(keyword, orderOfTopic)); + } else { + setTopicList(helpDocsManager.findTopicList("", orderOfTopic)); + } + mAdapter.notifyDataSetChanged(); + showTopicList(); + } else if (savedInstanceState.getBoolean(IS_WEBVIEW_SHOWN)) { + mWebView.loadUrl(savedInstanceState.getString(WEBVIEW_URL)); + showWebView(); + } + } + + @Override + protected void onSaveInstanceState(Bundle outState) { + outState.putIntegerArrayList(ORDER_OF_TOPIC, orderOfTopic); + outState.putString(TITLE, this.getTitle().toString()); + outState.putBoolean(IS_WEBVIEW_SHOWN, mWebView.isShown()); + outState.putBoolean(IS_LISTVIEW_SHOWN, mTopicListView.isShown()); + if (mContentsMenu != null) + outState.putBoolean(IS_CONTENTMENU_SHOWN, mContentsMenu.isVisible()); + if (keyword != null && !keyword.equals("")) + outState.putString(SEARCH_KEYWORD, keyword); + if (mWebView.isShown()) + outState.putString(WEBVIEW_URL, mWebView.getOriginalUrl()); + } + + @Override + public void onBackPressed() { + goBack(); + } + + /** Return the web view to the home page. */ + private void goHome() { + setTitle(getString(R.string.help_title)); + mWebView.clearView(); + mContentsMenu.setVisible(false); + mWebView.setVisibility(View.GONE); + mTopicListView.setVisibility(View.VISIBLE); + setTopicList(helpDocsManager.get1stLevelTopics()); + mAdapter.notifyDataSetChanged(); + } + + private void goBack() { + if (mWebView.isShown()) { + showTopicList(); + if (!orderOfTopic.isEmpty()) { + if (keyword == null) + keyword = ""; + setTopicList(helpDocsManager.findTopicList(keyword, orderOfTopic)); + } else if (keyword != null) { + setTopicList(helpDocsManager.searchHelp(keyword)); + } + showContentMenu(mTopicList.get(0)); + setListTitle(mTopicList.get(0)); + mAdapter.notifyDataSetChanged(); + } else { + if (helpDocsManager.is1stLevelTopic(mTopicList.get(0))) { + orderOfTopic.clear(); + finish(); + } else { + showContentMenu(mTopicList.get(0).getParentTopic()); + setListTitle(mTopicList.get(0).getParentTopic()); + if (!orderOfTopic.isEmpty()) { + orderOfTopic.remove(orderOfTopic.size() - 1); + if (keyword == null) + keyword = ""; + setTopicList(helpDocsManager.findTopicList(keyword, orderOfTopic)); + } else if (keyword != null) { + // setTopicList(helpDocsManager.searchHelp(keyword)); + finish(); + } + mAdapter.notifyDataSetChanged(); + } + } + } + + @Override + public boolean onTouchEvent(MotionEvent event) { + if (event.getAction() == MotionEvent.ACTION_DOWN) { + hideSoftKeyboard(HelpActivity.this); + } + return false; + } + + public static void hideSoftKeyboard(Activity activity) { + InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE); + inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0); + } + + public void setupUI(View view) { + view.setOnTouchListener(new OnTouchListener() { + + @Override + public boolean onTouch(View v, MotionEvent event) { + hideSoftKeyboard(HelpActivity.this); + return false; + } + }); + + if (view instanceof ViewGroup) { + for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) { + View innerView = ((ViewGroup) view).getChildAt(i); + setupUI(innerView); + } + } + } + +} diff --git a/help/HelpDocsManager.java b/help/HelpDocsManager.java new file mode 100644 index 0000000..1921dc2 --- /dev/null +++ b/help/HelpDocsManager.java @@ -0,0 +1,124 @@ +package com.sas.android.bimobile.ui.help; + +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import android.content.Context; + +import com.sas.android.bimobile.R; +import com.sas.android.bimobile.ui.share.HtmlMailTemplateParser; + +public class HelpDocsManager { + + private static String ALL_TOPICS = "alltopics"; + private static String TITLE = "title"; + private static String HTML = "html"; + private static String SUB_TOPICS = "subtopics"; + + private HelpTopic mRootTopic; + private ArrayList mResultList; + private Context mContext; + + public HelpDocsManager(Context context) { + mResultList = new ArrayList(); + mContext = context; + } + + public void parseHelpTopics(String helpTopicsJSON) { + try { + JSONArray allTopics = new JSONObject(helpTopicsJSON).getJSONArray(ALL_TOPICS); + if (mRootTopic == null) + mRootTopic = new HelpTopic(); + mRootTopic.setTitle(mContext.getString(R.string.help_title)); + getHelpTopic(allTopics, mRootTopic); + } catch (JSONException e) { + e.printStackTrace(); + } + } + + private void getHelpTopic(JSONArray topics, HelpTopic parentTopic) throws JSONException { + for (int i = 0; i < topics.length(); i++) { + JSONObject topic = topics.optJSONObject(i); + HelpTopic helpTopic = new HelpTopic(); + helpTopic.setTitle(topic.optString(TITLE)); + helpTopic.setHtml(topic.optString(HTML)); + helpTopic.setFileContent(getFileContent(topic.optString(HTML))); + helpTopic.setParentTopic(parentTopic); + parentTopic.addSubTopic(helpTopic); + JSONArray subTopics = topic.optJSONArray(SUB_TOPICS); + if (subTopics != null) { + getHelpTopic(subTopics, helpTopic); + } + } + } + + public ArrayList searchHelp(String keyword) { + if (keyword.trim().equals("")) { + return mRootTopic.getSubTopics(); + } + mResultList.clear(); + helpTopicContainKeyword(mRootTopic, keyword); + mResultList.remove(mRootTopic); + return mResultList; + } + + public ArrayList findTopicList(String keyword, ArrayList intArray) { + ArrayList topicList = new ArrayList(); + // if (keyword == null || keyword.equals("")) + topicList = searchHelp(keyword); + // else + // topicList = mRootTopic; + for (int i = 0; i < intArray.size(); i++) { + topicList = topicList.get(intArray.get(i)).getSubTopics(); + } + return topicList; + } + + private void helpTopicContainKeyword(HelpTopic topic, String keyword) { + if (topic.isContainKeyword(keyword)) + mResultList.add(topic); + if (topic.getSubTopics() != null) { + for (HelpTopic subTopic : topic.getSubTopics()) { + helpTopicContainKeyword(subTopic, keyword); + } + } + } + + public boolean is1stLevelTopic(HelpTopic topic) { + return topic.getParentTopic().getParentTopic() == null; + } + + public ArrayList get1stLevelTopics() { + return mRootTopic.getSubTopics(); + } + + public ArrayList getSubTopics(HelpTopic topic) { + return topic.getSubTopics(); + } + + public String getFileContent(String html) { + if (!(html + "").equals("")) { + try { + InputStream stream = mContext.getResources().getAssets().open( + mContext.getString(R.string.help_path) + html + ".html"); + String htmlContent = HtmlMailTemplateParser.inputStream2String(stream); + return android.text.Html.fromHtml(htmlContent).toString(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return ""; + } + + public ArrayList getParentTopics(HelpTopic topic) { + if (topic.getParentTopic().getParentTopic() != null) + return topic.getParentTopic().getParentTopic().getSubTopics(); + else + return mRootTopic.getSubTopics(); + } +} diff --git a/help/HelpTopic.java b/help/HelpTopic.java new file mode 100644 index 0000000..b5942eb --- /dev/null +++ b/help/HelpTopic.java @@ -0,0 +1,71 @@ +package com.sas.android.bimobile.ui.help; + +import java.util.ArrayList; +import java.util.Locale; + +public class HelpTopic { + + private String mTitle; + private String mHtml; + private String mFileContent; + private ArrayList mSubTopics; + private HelpTopic mParentTopic; + + public HelpTopic() { + mSubTopics = new ArrayList(); + } + + public boolean isContainKeyword(String keyword) { + // uppercase; + return (mFileContent + "").toUpperCase(Locale.getDefault()).indexOf( + keyword.toUpperCase(Locale.getDefault())) >= 0 + || (mTitle + "").toUpperCase(Locale.getDefault()).indexOf( + keyword.toUpperCase(Locale.getDefault())) >= 0; + } + + public String getTitle() { + return mTitle; + } + + public void setTitle(String mTitle) { + this.mTitle = mTitle; + } + + public String getHtml() { + return mHtml; + } + + public void setHtml(String mHtml) { + this.mHtml = mHtml; + } + + public ArrayList getSubTopics() { + if (mSubTopics != null) { + if (mSubTopics.size() == 0) { + return null; + } + } + return mSubTopics; + } + + public void addSubTopic(HelpTopic subTopic) { + this.mSubTopics.add(subTopic); + } + + public HelpTopic getParentTopic() { + return mParentTopic; + } + + public void setParentTopic(HelpTopic mParentTopic) { + this.mParentTopic = mParentTopic; + } + + public String getFileContent() { + return mFileContent; + } + + public void setFileContent(String mFileContent) { + this.mFileContent = mFileContent; + } + +} diff --git a/help/HelpTopicListAdapter.java b/help/HelpTopicListAdapter.java new file mode 100644 index 0000000..fcba042 --- /dev/null +++ b/help/HelpTopicListAdapter.java @@ -0,0 +1,59 @@ +package com.sas.android.bimobile.ui.help; + +import java.util.ArrayList; + +import android.content.Context; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.BaseAdapter; +import android.widget.TextView; + +import com.sas.android.bimobile.R; + +public class HelpTopicListAdapter extends BaseAdapter { + + private ArrayList mTopicsList; + private LayoutInflater mLayoutInflater; + + public HelpTopicListAdapter(Context context, ArrayList topicsList) { + mTopicsList = topicsList; + mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); + } + + @Override + public int getCount() { + return mTopicsList.size(); + } + + @Override + public HelpTopic getItem(int position) { + return mTopicsList.get(position); + } + + @Override + public long getItemId(int position) { + return position; + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + ViewHolder holder; + // if (convertView == null) { + convertView = mLayoutInflater.inflate(R.layout.help_listitem, null); + holder = new ViewHolder(); + holder.title = (TextView) convertView.findViewById(R.id.help_title); + holder.title.setText(mTopicsList.get(position).getTitle()); + convertView.setTag(holder); + // } else { + // holder = (ViewHolder) convertView.getTag(); + // } + return convertView; + } + + private class ViewHolder { + + TextView title; + } + +} diff --git a/share/AnnotationMailTemplate.java b/share/AnnotationMailTemplate.java new file mode 100644 index 0000000..bfc4816 --- /dev/null +++ b/share/AnnotationMailTemplate.java @@ -0,0 +1,27 @@ +package com.sas.android.bimobile.ui.share; + +import java.text.SimpleDateFormat; +import java.util.Date; + +import android.annotation.SuppressLint; +import android.content.Context; + +public class AnnotationMailTemplate extends SharedURLMailTemplate { + + public AnnotationMailTemplate(Context context) { + super(context); + } + + @SuppressLint("SimpleDateFormat") + public String getFullFileName() { + SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss"); + Date curDate = new Date(System.currentTimeMillis()); + String date = formatter.format(curDate); + return getReportName().replaceAll(" ", "") + "_" + getSectionName().replaceAll(" ", "") + + "_" + date + ".png"; + } + + public String getShortFileName() { + return getReportName().replaceAll(" ", "") + ".png"; + } +} diff --git a/share/CommonMailTemplate.java b/share/CommonMailTemplate.java new file mode 100644 index 0000000..1cdb4f8 --- /dev/null +++ b/share/CommonMailTemplate.java @@ -0,0 +1,72 @@ +package com.sas.android.bimobile.ui.share; + +import android.content.Context; +import android.content.Intent; +import android.net.Uri; + +import com.sas.android.bimobile.R; + +public class CommonMailTemplate extends MailInfoEntity implements IMailable { + + protected Context mContext; + + public CommonMailTemplate(Context context) { + this.mContext = context; + } + + @Override + public String composeSubject() { + return mContext.getString(R.string.mail_template_review, getReportName()); + + } + + @Override + public String composeText() { + String text = ""; + if (getReportName() != null) + text += "\n" + mContext.getString(R.string.mail_template_report_name, getReportName()); + if (getAuthor() != null) + text += "\n" + "\n" + mContext.getString(R.string.mail_template_author, getAuthor()); + if (getLastModifyDate() != null) + text += "\n" + mContext.getString(R.string.mail_template_date, getLastModifyDate()); + if (getDescription() != null) + text += "\n" + mContext.getString(R.string.mail_template_description, getDescription()); + if (getReportSectionLink() != null) + text += "\n\n\n" + + mContext.getString(R.string.mail_template_url, getReportName(), + getReportSectionLink()); + return text; + } + + @Override + public Uri composeAttachment() { + Uri uri = null; + if (mFilePath != null) + uri = Uri.parse("file:////" + mFilePath); + return uri; + } + + @Override + public String composeAttachmentType() { + return "message/rfc822"; + // if (mFilePath != null) + // return "image/png"; + // else + // return "text/plain"; + } + + @Override + public void send(Context context) { + Intent intent = new Intent(Intent.ACTION_SEND); + + intent.putExtra(Intent.EXTRA_SUBJECT, composeSubject()); + intent.putExtra(Intent.EXTRA_TEXT, composeText()); + if (mFilePath != null) + intent.putExtra(Intent.EXTRA_STREAM, composeAttachment()); + intent.setType(composeAttachmentType()); + + context.startActivity(Intent.createChooser(intent, + context.getString(R.string.mail_template_share_via))); + } + +} diff --git a/share/HtmlMailTemplateParser.java b/share/HtmlMailTemplateParser.java new file mode 100644 index 0000000..bc969f8 --- /dev/null +++ b/share/HtmlMailTemplateParser.java @@ -0,0 +1,196 @@ +package com.sas.android.bimobile.ui.share; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; + +import android.content.Context; + +import com.sas.android.bimobile.R; + +public class HtmlMailTemplateParser { + + private static final String KEY_WORD_REPORT_SECTION_LINK = "$$REPORT_SECTION_LINK$$"; + private static final String KEY_WORD_REPORT_SECTION_UNLINK = "$$REPORT_SECTION_UNLINK$$"; + private static final String KEY_WORD_REPRT_SECTION_LINK_CONTENT = "$$REPORT_SECTION_LINK_CONTENT$$"; + private static final String KEY_WORD_REPORT_NAME = "$$REPORT_NAME$$"; + private static final String KEY_WORD_FROM = "$$FROM$$"; + private static final String KEY_WORD_USERID = "$$USERID$$"; + private static final String KEY_WORD_AUTHOR = "$$AUTHOR$$"; + private static final String KEY_WORD_LAST_MODIFIED = "$$LAST_MODIFIED$$"; + private static final String KEY_WORD_DESCRIPTION = "$$DESCRIPTION$$"; + private static final String KEY_WORD_REPORT_THUMBNAIL_SRC = "$$REPORT_THUMBNAIL_SRC$$"; + private static final String KEY_WORD_URL_TIPS = "$$REPORT_LINK_TIPS$$"; + private static final String KEY_WORD_REVIEW_TIP = "$$REVIEW_TIP$$"; + private static final String KEY_WORD_FROM_LABEL = "$$FROM_LABEL$$"; + private static final String KEY_WORD_AUTHOR_LABEL = "$$AUTHOR_LABEL$$"; + private static final String KEY_WORD_DATE_LABEL = "$$DATE_LABEL$$"; + private static final String KEY_WORD_DESCRIPTION_LABEL = "$$DESCRIPTION_LABEL$$"; + + private String mHtmlTemplateContent; + private Context mContext; + private String mDefaultString; + + public HtmlMailTemplateParser(Context context, InputStream stream) { + this(context, inputStream2String(stream)); + + } + + public HtmlMailTemplateParser(Context context, String content) { + this.mHtmlTemplateContent = content; + mContext = context; + mDefaultString = mContext.getString(R.string.mail_template_not_provided); + replaceFixedTextLabels(); + + } + + public static String inputStream2String(InputStream stream) { + BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); + StringBuilder sb = new StringBuilder(); + String line = null; + try { + while ((line = reader.readLine()) != null) { + sb.append(line); + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + stream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return sb.toString(); + } + + private void replaceReviewTip() { + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_REVIEW_TIP, + mContext.getString(R.string.mail_template_review_tip)); + } + + private void replaceFromLabel() { + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_FROM_LABEL, + mContext.getString(R.string.mail_template_from_label)); + } + + private void replaceAuthorLabel() { + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_AUTHOR_LABEL, + mContext.getString(R.string.mail_template_author_label)); + } + + private void replaceDateLabel() { + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_DATE_LABEL, + mContext.getString(R.string.mail_template_date_label)); + } + + private void replaceDescriptionLabel(String description) { + if (description == null || description == "") + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_DESCRIPTION_LABEL, ""); + else + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_DESCRIPTION_LABEL, + mContext.getString(R.string.mail_template_description_label)); + } + + private void replaceFixedTextLabels() { + replaceReviewTip(); + replaceFromLabel(); + replaceAuthorLabel(); + replaceDateLabel(); + //replaceDescriptionLabel(); + } + + private void replaceReportSectionLink(String reportSectionLink) { + String reportSectionLinkHtml = ""; + String reportSectionUnLinkHtml = ""; + if (reportSectionLink != null) { + reportSectionLinkHtml = mContext.getString(R.string.mail_templete_url_link, + reportSectionLink); + reportSectionUnLinkHtml = mContext.getString(R.string.mail_templete_url_unlink, + reportSectionLink); + } else { + reportSectionLinkHtml = ""; + reportSectionUnLinkHtml = ""; + } + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_REPORT_SECTION_LINK, + reportSectionLinkHtml); + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_REPORT_SECTION_UNLINK, + reportSectionUnLinkHtml); + } + + private void replaceReportSectionLinkContent(String reportSectionLinkContent) { + if (reportSectionLinkContent == null) + reportSectionLinkContent = ""; + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_REPRT_SECTION_LINK_CONTENT, + reportSectionLinkContent); + } + + private void replaceReportName(String reportName) { + if (reportName == null) + reportName = mDefaultString; + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_REPORT_NAME, reportName); + } + + private void replaceFrom(String from) { + if (from == null) + from = mContext.getString(R.string.mail_template_no_displayName); + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_FROM, from); + } + + private void replaceUserID(String userID) { + if (userID == null) + userID = ""; + else + userID = "(" + userID + ")"; + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_USERID, userID); + } + + private void replaceAuthor(String author) { + if (author == null) + author = mDefaultString; + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_AUTHOR, author); + } + + private void replaceLastModified(String lastModified) { + if (lastModified == null) + lastModified = mDefaultString; + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_LAST_MODIFIED, lastModified); + } + + private void replaceDiscriptionLabel(String discriptionLabel) { + if (discriptionLabel == null || discriptionLabel.equals("")) { + discriptionLabel = ""; + } + replaceDescriptionLabel(discriptionLabel); + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_DESCRIPTION, discriptionLabel); + } + + private void replaceReportThumbnailSrc(String reportThumbnailSrc) { + if (reportThumbnailSrc == null) + reportThumbnailSrc = ""; + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_REPORT_THUMBNAIL_SRC, + reportThumbnailSrc); + } + + private void replaceReportURLTips(Context context, String reportURL) { + String tips = ""; + if (reportURL != null) + tips = context.getString(R.string.mail_template_url_tips); + mHtmlTemplateContent = mHtmlTemplateContent.replace(KEY_WORD_URL_TIPS, tips); + } + + public String parseHtmlTemplate(MailInfoEntity entity) { + replaceReportSectionLink(entity.getReportSectionLink()); + replaceReportSectionLinkContent(entity.getReportSectionLink()); + replaceReportName(entity.getReportName()); + replaceFrom(entity.getFrom()); + replaceUserID(entity.getmUserID()); + replaceAuthor(entity.getAuthor()); + replaceLastModified(entity.getLastModifyDate()); + replaceDiscriptionLabel(entity.getDescription()); + replaceReportThumbnailSrc(entity.getReportThumbnailSrc()); + replaceReportURLTips(mContext, entity.getReportSectionLink()); + return mHtmlTemplateContent; + } +} diff --git a/share/IMailable.java b/share/IMailable.java new file mode 100644 index 0000000..590f788 --- /dev/null +++ b/share/IMailable.java @@ -0,0 +1,18 @@ +package com.sas.android.bimobile.ui.share; + +import android.content.Context; +import android.net.Uri; + +public interface IMailable { + + public abstract String composeSubject(); + + public abstract String composeText(); + + public abstract Uri composeAttachment(); + + public abstract String composeAttachmentType(); + + public abstract void send(Context context); + +} diff --git a/share/ImageUtilities.java b/share/ImageUtilities.java new file mode 100644 index 0000000..6013547 --- /dev/null +++ b/share/ImageUtilities.java @@ -0,0 +1,86 @@ +package com.sas.android.bimobile.ui.share; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +import android.graphics.Bitmap; +import android.graphics.Bitmap.CompressFormat; +import android.graphics.BitmapFactory; +import android.util.Base64; + +public class ImageUtilities { + + // should be moved to com.sas.android.bimobile.util.ImageUtil? + + private final static int MAX_IMAGE_SIZE = 1024; + + private static boolean isNeedSizeChanged(Bitmap src) { + return src.getWidth() > MAX_IMAGE_SIZE || src.getHeight() > MAX_IMAGE_SIZE; + } + + public static Bitmap convertBitmap(Bitmap src) { + if (isNeedSizeChanged(src)) { + int width = src.getWidth(); + int height = src.getHeight(); + if (height <= width && width > MAX_IMAGE_SIZE) { + height = height * MAX_IMAGE_SIZE / width; + width = MAX_IMAGE_SIZE; + } else if (height > width && height > MAX_IMAGE_SIZE) { + width = width * MAX_IMAGE_SIZE / height; + height = MAX_IMAGE_SIZE; + } + return Bitmap.createScaledBitmap(src, width, height, true); + } else { + return Bitmap.createBitmap(src); + } + } + + public static void convertBitmap(String filePath) { + Bitmap src = BitmapFactory.decodeFile(filePath); + if (isNeedSizeChanged(src)) { + Bitmap dst = convertBitmap(src); + recycleBitmap(src); + File file = new File(filePath); + if (file.exists()) { + file.delete(); + } + try { + FileOutputStream fos = new FileOutputStream(file); + dst.compress(filePath.endsWith("png") ? CompressFormat.PNG : CompressFormat.JPEG, + 100, fos); + fos.flush(); + fos.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + public static String img2Base64(String filepath) { + Bitmap src = BitmapFactory.decodeFile(filepath); + Bitmap dst = src; + if (isNeedSizeChanged(src)) { + dst = convertBitmap(src); + recycleBitmap(src); + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + dst.compress(Bitmap.CompressFormat.JPEG, 100, baos); + byte[] byteArrayImage = baos.toByteArray(); + String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT); + return encodedImage; + } + + private static void recycleBitmap(Bitmap bitmap) { + if (bitmap != null && !bitmap.isRecycled()) { + bitmap.recycle(); + bitmap = null; + System.gc(); + } + } + +} diff --git a/share/MailInfoEntity.java b/share/MailInfoEntity.java new file mode 100644 index 0000000..74b4ae2 --- /dev/null +++ b/share/MailInfoEntity.java @@ -0,0 +1,98 @@ +package com.sas.android.bimobile.ui.share; + +public abstract class MailInfoEntity { + + protected String mReportName; + protected String mSectionName; + protected String mFrom; + protected String mUserID; + protected String mAuthor; + protected String mLastModifyDate; + protected String mDescription; + protected String mFilePath; + protected String mReportSectionLink; + protected String mReportThumbnailSrc; + + public String getReportName() { + if (mReportName.indexOf("(Report)") > 0) { + mReportName = mReportName.substring(0, mReportName.indexOf("(Report)")); + } + return mReportName; + } + + public void setReportName(String reportName) { + this.mReportName = reportName; + } + + public String getSectionName() { + return mSectionName; + } + + public void setSectionName(String sectionName) { + this.mSectionName = sectionName; + } + + public String getAuthor() { + return mAuthor; + } + + public void setAuthor(String author) { + this.mAuthor = author; + } + + public String getLastModifyDate() { + return mLastModifyDate; + } + + public void setLastModifyDate(String lastModifyDate) { + this.mLastModifyDate = lastModifyDate; + } + + public String getDescription() { + return mDescription; + } + + public void setDescription(String description) { + this.mDescription = description; + } + + public String getFilePath() { + return mFilePath; + } + + public void setFilePath(String filePath) { + this.mFilePath = filePath; + } + + public String getFrom() { + return mFrom; + } + + public void setFrom(String from) { + this.mFrom = from; + } + + public String getmUserID() { + return mUserID; + } + + public void setmUserID(String mUserID) { + this.mUserID = mUserID; + } + + public String getReportSectionLink() { + return mReportSectionLink; + } + + public void setReportSectionLink(String reportSectionLink) { + this.mReportSectionLink = reportSectionLink; + } + + public String getReportThumbnailSrc() { + return mReportThumbnailSrc; + } + + public void setReportThumbnailSrc(String reportThumbnailSrc) { + this.mReportThumbnailSrc = reportThumbnailSrc; + } +} diff --git a/share/SharedURLMailTemplate.java b/share/SharedURLMailTemplate.java new file mode 100644 index 0000000..3c27322 --- /dev/null +++ b/share/SharedURLMailTemplate.java @@ -0,0 +1,51 @@ +package com.sas.android.bimobile.ui.share; + +import java.io.IOException; +import java.io.InputStream; + +import android.content.Context; +import android.content.Intent; +import android.text.Html; + +import com.sas.android.bimobile.R; + +public class SharedURLMailTemplate extends CommonMailTemplate { + + public SharedURLMailTemplate(Context context) { + super(context); + } + + @Override + public String composeText() { + return parseHTML(mContext, mFilePath); + } + + @Override + public void send(final Context context) { + Intent intent = new Intent(Intent.ACTION_SEND); + + intent.putExtra(Intent.EXTRA_SUBJECT, composeSubject()); + intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(composeText())); + if (mFilePath != null) + intent.putExtra(Intent.EXTRA_STREAM, composeAttachment()); + intent.setType(composeAttachmentType()); + + context.startActivity(Intent.createChooser(intent, + context.getString(R.string.mail_template_share_via))); + } + + public String parseHTML(Context context, String filepath) { + String parsed = ""; + // setReportThumbnailSrc("data:image/png;base64," + ImageUtilities.img2Base64(filepath)); + try { + InputStream stream = context.getResources().getAssets().open( + "template/EmailTemplate.htm"); + HtmlMailTemplateParser parser = new HtmlMailTemplateParser(context, stream); + parsed = parser.parseHtmlTemplate(this); + } catch (IOException e) { + e.printStackTrace(); + } + return parsed; + } + +}