Advertisement
Guest User

NOTE, BOOKMARK, CONTENT

a guest
Nov 25th, 2015
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 37.84 KB | None | 0 0
  1. //----------------------------NOTE FRAGMENT
  2. package com.codeid.mosproject;
  3.  
  4. import android.os.Bundle;
  5. import android.support.annotation.Nullable;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v7.widget.Toolbar;
  8. import android.view.LayoutInflater;
  9. import android.view.View;
  10. import android.view.ViewGroup;
  11.  
  12.  
  13. /**
  14.  * Created by INDRA PRAYOGI on 03/11/2015.
  15.  */
  16. public class NotesFragment extends Fragment {
  17.  
  18.     @Nullable
  19.     @Override
  20.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  21.         return inflater.inflate(R.layout.fragment_notes,null);
  22.     }
  23.  
  24.     @Override
  25.     public void onViewCreated(View view, Bundle savedInstanceState) {
  26.         super.onViewCreated(view, savedInstanceState);
  27.         Toolbar toolbar = (Toolbar) (this.getActivity()).findViewById(R.id.toolbar);
  28.         toolbar.setTitle("Notes");
  29.     }
  30. }
  31.  
  32.  
  33. //--------------------------------------------- CONTENT
  34. package com.codeid.mosproject;
  35.  
  36. import android.app.AlertDialog;
  37. import android.app.Dialog;
  38. import android.app.ProgressDialog;
  39. import android.content.DialogInterface;
  40. import android.content.Intent;
  41. import android.graphics.Bitmap;
  42. import android.os.AsyncTask;
  43. import android.os.Bundle;
  44. import android.support.annotation.Nullable;
  45. import android.support.v4.app.Fragment;
  46. import android.support.v4.view.MenuItemCompat;
  47. import android.support.v7.widget.SearchView;
  48. import android.support.v7.widget.Toolbar;
  49. import android.util.Log;
  50. import android.view.Gravity;
  51. import android.view.LayoutInflater;
  52. import android.view.Menu;
  53. import android.view.MenuInflater;
  54. import android.view.MenuItem;
  55. import android.view.View;
  56. import android.view.ViewGroup;
  57. import android.widget.AdapterView;
  58. import android.widget.ArrayAdapter;
  59. import android.widget.GridView;
  60. import android.widget.LinearLayout;
  61. import android.widget.ListView;
  62. import android.widget.TextView;
  63. import android.widget.Toast;
  64.  
  65. import com.codeid.mosproject.Database.Bookmark;
  66. import com.codeid.mosproject.Database.Content;
  67. import com.codeid.mosproject.Database.DatabaseHandler;
  68. import com.codeid.mosproject.Database.Note;
  69. import com.codeid.mosproject.Helper.ConnectionDetector;
  70. import com.codeid.mosproject.Helper.ContentAdapter;
  71. import com.codeid.mosproject.Helper.CustomGrid;
  72.  
  73. import org.apache.http.HttpResponse;
  74. import org.apache.http.client.ClientProtocolException;
  75. import org.apache.http.client.HttpClient;
  76. import org.apache.http.client.methods.HttpPost;
  77. import org.apache.http.entity.StringEntity;
  78. import org.apache.http.impl.client.BasicCookieStore;
  79. import org.apache.http.impl.client.DefaultHttpClient;
  80. import org.apache.http.params.BasicHttpParams;
  81. import org.apache.http.params.HttpConnectionParams;
  82. import org.apache.http.params.HttpProtocolParams;
  83. import org.apache.http.protocol.BasicHttpContext;
  84. import org.apache.http.protocol.HttpContext;
  85. import org.apache.http.util.EntityUtils;
  86. import org.json.JSONException;
  87. import org.json.JSONObject;
  88.  
  89. import java.io.IOException;
  90. import java.io.UnsupportedEncodingException;
  91. import java.net.SocketTimeoutException;
  92. import java.net.URLEncoder;
  93. import java.text.SimpleDateFormat;
  94. import java.util.ArrayList;
  95. import java.util.Date;
  96. import java.util.Locale;
  97.  
  98.  
  99. /**
  100.  * Created by INDRA PRAYOGI on 03/11/2015.
  101.  */
  102. public class ContentFragment extends Fragment {
  103.  
  104.     DatabaseHandler handler;
  105.     ContentAdapter adapter;
  106.     ListView listView;
  107.     TextView txtEmpty;
  108.     SearchView searchView=null;
  109.     android.support.v4.app.FragmentManager mFragmentManager;
  110.     public static final String EXTRA_DEFAULT_INTENT = "com.codeid.mosproject.EXTRA_DEFAULT_INTENT";
  111.     final ArrayList<Content> contentList=null;
  112.     String pointer="";
  113.     private HttpClient httpclient;
  114.     private HttpContext localContext;
  115.     private HttpResponse response;
  116.     Bitmap bitmap;
  117.     String encodeImage="";
  118.     private String errMsg = "";
  119.     private int errCode = 0;
  120.     private int resultfinal = 0;
  121.     private ProgressDialog pDialog;
  122.     private ConnectionDetector cd;
  123.     private Boolean isInternetPresent = false;
  124.     String idArticleBookmark="";
  125.     String  bookmarkID="";
  126.     ArrayList<Note> noteList=null;
  127.     ArrayList<Bookmark> bookmarkList=null;
  128.  
  129.     //-----------set item grid
  130.     String[] menu = {
  131.             "mail",
  132.             "add bookmark",
  133.             "add note",
  134.  
  135.     } ;
  136.     int[] imageId = {
  137.             R.drawable.btn_mailblue,
  138.             R.drawable.btn_addbookmark,
  139.             R.drawable.btn_addnote,
  140.     };
  141.     @Nullable
  142.     @Override
  143.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  144.         return inflater.inflate(R.layout.fragment_content,null);
  145.  
  146.     }
  147.  
  148.     @Override
  149.     public void onViewCreated(final View view, Bundle savedInstanceState) {
  150.         super.onViewCreated(view, savedInstanceState);
  151.         Toolbar toolbar  = (Toolbar) (this.getActivity()).findViewById(R.id.toolbar);
  152.         toolbar.setTitle("Content");
  153.         handler = new DatabaseHandler(getActivity());
  154.         listView = (ListView) view.findViewById(R.id.listview);
  155.         txtEmpty = (TextView) view.findViewById(R.id.empty);
  156.         txtEmpty.setVisibility(View.INVISIBLE);
  157.         final ArrayList<Content> contentList = handler.getAllContentByVolume();
  158.         noteList = handler.getAllNote();
  159.         bookmarkList = handler.getAllBookmark();
  160.  
  161.  
  162.  
  163.         if(contentList.size()==0)
  164.         {
  165.             txtEmpty.setVisibility(View.VISIBLE);
  166.         }
  167.         adapter = new ContentAdapter(getActivity(),contentList);
  168.         listView.setAdapter(adapter);
  169.  
  170.         listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  171.             @Override
  172.             public void onItemClick(AdapterView<?> parent, View view,
  173.                                     int position, long id) {
  174.                 /*Toast.makeText(getActivity().getApplicationContext(),
  175.                         "Click ListItem Number " + position, Toast.LENGTH_LONG)
  176.                         .show();*/
  177.                 Content content = contentList.get(position);
  178.                 Intent intent;
  179.                 intent = new Intent(getActivity(), ContentListActivity.class);
  180.                 intent.putExtra(EXTRA_DEFAULT_INTENT, 0);
  181.                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  182.                 intent.putExtra("title", content.getTitle());
  183.                 intent.putExtra("volume", content.getVolumeCode());
  184.                 startActivity(intent);
  185.                 getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
  186.  
  187.             }
  188.         });
  189.  
  190.         listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  191.             @Override
  192.             public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
  193.                                            int pos, long id) {
  194.                 // TODO Auto-generated method stub
  195.                 final Content content = contentList.get(pos);
  196.                 final String contenttitelInfo = content.getTitle();
  197.                 final String contentdetailInfo = content.getContent();
  198.                 final String contentdateInfo = content.getCreatedDate();
  199.                 final String articlecode = content.getArticleCode();
  200.                 Note note=null;
  201.                 Bookmark bookmark=null;
  202.                 pointer = content.getVolumeCode();
  203.  
  204.  
  205.                 int count,countbookmark;
  206.                 int haveNote=0;
  207.                 int haveBookmark=0;
  208.                 int _isActive=0;
  209.                 int _isActiveBookmark=0;
  210.                 for(count=0;noteList.size()>count;count++)
  211.                 {
  212.                     if(noteList.get(count).getIdArticle().equalsIgnoreCase(articlecode)) {
  213.                         haveNote = 1;
  214.                         note = noteList.get(count);
  215.                         _isActive=note.getIsActive();
  216.                         Log.d("Ini is Active", String.valueOf(_isActive));
  217.                         count = noteList.size();
  218.                     }
  219.                 }
  220.  
  221.                 for(countbookmark=0;bookmarkList.size()>countbookmark;countbookmark++)
  222.                 {
  223.                     if(bookmarkList.get(countbookmark).getIdArticle().equalsIgnoreCase(articlecode)) {
  224.                         haveBookmark = 1;
  225.                         bookmark = bookmarkList.get(countbookmark);
  226.                         _isActiveBookmark=bookmark.getIsActive();
  227.                         Log.d("Ini is Active bookmark", String.valueOf(_isActive));
  228.                         countbookmark = bookmarkList.size();
  229.                     }
  230.                 }
  231.  
  232.                 //------ IF HAVE NOTE BUT NOT HAVE BOOKMARK
  233.                 if (note!=null&&haveNote==1&&_isActive==1&&bookmark==null)
  234.                 {
  235.                     final String _text = note.getText();
  236.                     final String _idNote = note.getIdNote();
  237.                     final String _imageNote = note.getImage();
  238.                     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  239.                     ArrayList<String> arrayList = new ArrayList<String>();
  240.                     arrayList.add("Show Article Info");
  241.                     arrayList.add("Show Note");
  242.                     arrayList.add("Delete Note");
  243.                     arrayList.add("Add Bookmark");
  244.                     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
  245.                     DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
  246.  
  247.                         @Override
  248.                         public void onClick(DialogInterface dialog, int which) {
  249.                             switch (which) {
  250.                                 case 0:
  251.                                     openBottomSheet(view,contenttitelInfo, contentdetailInfo, contentdateInfo);
  252.                                     break;
  253.                                 case 1:
  254.                                     Intent intent;
  255.                                     intent = new Intent(getActivity(), DetailNoteActivity.class);
  256.                                     intent.putExtra("title", content.getTitle());
  257.                                     intent.putExtra("text_", _text);
  258.                                     intent.putExtra("image_",_imageNote);
  259.                                     intent.putExtra("pointer_", pointer);
  260.                                     intent.putExtra("articleid", articlecode);
  261.                                     intent.putExtra("idNote", _idNote);
  262.                                     intent.putExtra(EXTRA_DEFAULT_INTENT, 0);
  263.                                     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  264.                                     startActivity(intent);
  265.                                     getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
  266.                                     break;
  267.  
  268.                                 case 2:
  269.                                     handler = new DatabaseHandler(getActivity());
  270.                                     AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
  271.                                     builder1.setMessage("Are you sure want to delete this note ?");
  272.                                     builder1.setCancelable(true);
  273.                                     builder1.setPositiveButton("Yes",
  274.                                             new DialogInterface.OnClickListener() {
  275.                                                 public void onClick(DialogInterface dialog, int id) {
  276.                                                     handler.DeleteNote(_idNote);
  277.                                                 }
  278.                                             });
  279.                                     builder1.setNegativeButton("No",
  280.                                             new DialogInterface.OnClickListener() {
  281.                                                 public void onClick(DialogInterface dialog, int id) {
  282.                                                     dialog.cancel();
  283.                                                 }
  284.                                             });
  285.                                     AlertDialog alert11 = builder1.create();
  286.                                     alert11.show();
  287.  
  288.                                     break;
  289.                                 case 3:
  290.                                     new PostBookmark().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  291.                                     idArticleBookmark=content.getArticleCode();
  292.                                     break;
  293.                                 default:
  294.                                     break;
  295.                             }
  296.                         }
  297.                     };
  298.                     builder.setAdapter(adapter, listener);
  299.                     builder.show();
  300.                 }
  301.                 //------ IF HAVE BOOKMARK BUT NOT HAVE NOTE
  302.                 else if (note==null&&bookmark!=null&&haveBookmark==1&&_isActiveBookmark==1)
  303.                 {
  304.                     final String _idBookmark = bookmark.getIdBookmark();
  305.                     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  306.                     ArrayList<String> arrayList = new ArrayList<String>();
  307.                     arrayList.add("Show Article Info");
  308.                     arrayList.add("Add Note");
  309.                     arrayList.add("Delete Bookmark");
  310.                     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
  311.                     DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
  312.  
  313.                         @Override
  314.                         public void onClick(DialogInterface dialog, int which) {
  315.                             switch (which) {
  316.                                 case 0:
  317.                                     openBottomSheet(view,contenttitelInfo, contentdetailInfo, contentdateInfo);
  318.                                     break;
  319.                                 case 1:
  320.                                     Intent intent;
  321.                                     intent = new Intent(getActivity(), AddNoteActivity.class);
  322.                                     intent.putExtra("title", content.getTitle());
  323.                                     intent.putExtra("content_", content.getContent());
  324.                                     intent.putExtra("pointer_", pointer);
  325.                                     intent.putExtra("articleid", content.getArticleCode());
  326.                                     Log.d("Yang dilempar", content.getArticleCode());
  327.                                     Log.d("CONTENT COY", content.getContent_En());
  328.                                     intent.putExtra(EXTRA_DEFAULT_INTENT, 0);
  329.                                     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  330.                                     startActivity(intent);
  331.                                     getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
  332.                                     break;
  333.                                 case 2:
  334.                                     handler = new DatabaseHandler(getActivity());
  335.                                     AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
  336.                                     builder1.setMessage("Are you sure want to delete this bookmark ?");
  337.                                     builder1.setCancelable(true);
  338.                                     builder1.setPositiveButton("Yes",
  339.                                             new DialogInterface.OnClickListener() {
  340.                                                 public void onClick(DialogInterface dialog, int id) {
  341.                                                     handler.DeleteBookmark(_idBookmark);
  342.                                                 }
  343.                                             });
  344.                                     builder1.setNegativeButton("No",
  345.                                             new DialogInterface.OnClickListener() {
  346.                                                 public void onClick(DialogInterface dialog, int id) {
  347.                                                     dialog.cancel();
  348.                                                 }
  349.                                             });
  350.                                     AlertDialog alert11 = builder1.create();
  351.                                     alert11.show();
  352.                                     break;
  353.                                 default:
  354.                                     break;
  355.                             }
  356.                         }
  357.                     };
  358.                     builder.setAdapter(adapter, listener);
  359.                     builder.show();
  360.                 }
  361.                 //----------- Have Bookmark AND Have Note
  362.                 else if(note!=null&&haveNote==1&&_isActive==1&&bookmark!=null&&haveBookmark==1&&_isActiveBookmark==1)
  363.                 {
  364.                     final String _text = note.getText();
  365.                     final String _idNote = note.getIdNote();
  366.                     final String _idBookmark = bookmark.getIdBookmark();
  367.                     final String _imageNote = note.getImage();
  368.                     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  369.                     ArrayList<String> arrayList = new ArrayList<String>();
  370.                     arrayList.add("Show Article Info");
  371.                     arrayList.add("Show Note");
  372.                     arrayList.add("Delete Note");
  373.                     arrayList.add("Delete Bookmark");
  374.                     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
  375.                     DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
  376.  
  377.                         @Override
  378.                         public void onClick(DialogInterface dialog, int which) {
  379.                             switch (which) {
  380.                                 case 0:
  381.                                     openBottomSheet(view,contenttitelInfo, contentdetailInfo, contentdateInfo);
  382.                                     break;
  383.                                 case 1:
  384.                                     Intent intent;
  385.                                     intent = new Intent(getActivity(), DetailNoteActivity.class);
  386.                                     intent.putExtra("title", content.getTitle());
  387.                                     intent.putExtra("text_", _text);
  388.                                     intent.putExtra("image_",_imageNote);
  389.                                     intent.putExtra("pointer_", pointer);
  390.                                     intent.putExtra("articleid", articlecode);
  391.                                     intent.putExtra("idNote", _idNote);
  392.                                     intent.putExtra(EXTRA_DEFAULT_INTENT, 0);
  393.                                     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  394.                                     startActivity(intent);
  395.                                     getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
  396.                                     break;
  397.  
  398.                                 case 2:
  399.                                     handler = new DatabaseHandler(getActivity());
  400.                                     AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
  401.                                     builder1.setMessage("Are you sure want to delete this note ?");
  402.                                     builder1.setCancelable(true);
  403.                                     builder1.setPositiveButton("Yes",
  404.                                             new DialogInterface.OnClickListener() {
  405.                                                 public void onClick(DialogInterface dialog, int id) {
  406.                                                     handler.DeleteNote(_idNote);
  407.                                                 }
  408.                                             });
  409.                                     builder1.setNegativeButton("No",
  410.                                             new DialogInterface.OnClickListener() {
  411.                                                 public void onClick(DialogInterface dialog, int id) {
  412.                                                     dialog.cancel();
  413.                                                 }
  414.                                             });
  415.                                     AlertDialog alert11 = builder1.create();
  416.                                     alert11.show();
  417.  
  418.                                     break;
  419.                                 case 3:
  420.                                     handler = new DatabaseHandler(getActivity());
  421.                                     AlertDialog.Builder builder2 = new AlertDialog.Builder(getActivity());
  422.                                     builder2.setMessage("Are you sure want to delete this bookmark ?");
  423.                                     builder2.setCancelable(true);
  424.                                     builder2.setPositiveButton("Yes",
  425.                                             new DialogInterface.OnClickListener() {
  426.                                                 public void onClick(DialogInterface dialog, int id) {
  427.                                                     handler.DeleteBookmark(_idBookmark);
  428.                                                 }
  429.                                             });
  430.                                     builder2.setNegativeButton("No",
  431.                                             new DialogInterface.OnClickListener() {
  432.                                                 public void onClick(DialogInterface dialog, int id) {
  433.                                                     dialog.cancel();
  434.                                                 }
  435.                                             });
  436.                                     AlertDialog alert12 = builder2.create();
  437.                                     alert12.show();
  438.                                     break;
  439.                                 default:
  440.                                     break;
  441.                             }
  442.                         }
  443.                     };
  444.                     builder.setAdapter(adapter, listener);
  445.                     builder.show();
  446.                 }
  447.                 //------ NOT HAVE NOTE & NOT HAVE BOOKMARK
  448.                 else {
  449.                     AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  450.                     ArrayList<String> arrayList = new ArrayList<String>();
  451.                     arrayList.add("Show Article Info");
  452.                     arrayList.add("Add Note");
  453.                     arrayList.add("Add Bookmark");
  454.                     ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, arrayList);
  455.                     DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
  456.  
  457.                         @Override
  458.                         public void onClick(DialogInterface dialog, int which) {
  459.                             switch (which) {
  460.                                 case 0:
  461.                                     openBottomSheet(view,contenttitelInfo, contentdetailInfo, contentdateInfo);
  462.                                     break;
  463.                                 case 1:
  464.                                     Intent intent;
  465.                                     intent = new Intent(getActivity(), AddNoteActivity.class);
  466.                                     intent.putExtra("title", content.getTitle());
  467.                                     intent.putExtra("content_", content.getContent());
  468.                                     intent.putExtra("pointer_", pointer);
  469.                                     intent.putExtra("articleid", content.getArticleCode());
  470.                                     Log.d("Yang dilempar", content.getArticleCode());
  471.                                     Log.d("CONTENT COY", content.getContent_En());
  472.                                     intent.putExtra(EXTRA_DEFAULT_INTENT, 0);
  473.                                     intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  474.                                     startActivity(intent);
  475.                                     getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
  476.                                     break;
  477.                                 case 2:
  478.                                     new PostBookmark().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  479.                                     idArticleBookmark=content.getArticleCode();
  480.                                     //Toast.makeText(getActivity(),"Bookmark has been added", Toast.LENGTH_SHORT).show();
  481.                                     break;
  482.                                 default:
  483.                                     break;
  484.                             }
  485.                         }
  486.                     };
  487.                     builder.setAdapter(adapter, listener);
  488.                     builder.show();
  489.                 }
  490.  
  491.                 return true;
  492.             }
  493.         });
  494.     }
  495.  
  496.     @Override
  497.     public void onCreateOptionsMenu(Menu menu, MenuInflater menuinflater) {
  498.         menu.clear();
  499.         menuinflater.inflate(R.menu.home, menu);
  500.         MenuItem searchItem = menu.findItem(R.id.action_search);
  501.         searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
  502.  
  503.         searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
  504.  
  505.             @Override
  506.             public boolean onQueryTextSubmit(String text) {
  507.                 Toast.makeText(getActivity().getApplicationContext(), "dddddd " + text, Toast.LENGTH_LONG).show();
  508.                 return false;
  509.             }
  510.  
  511.             @Override
  512.             public boolean onQueryTextChange(String text) {
  513.                 int textlength = text.length();
  514.  
  515.  
  516.                 ArrayList<Content> tempArrayList = new ArrayList<Content>();
  517.                 for (Content c : contentList) {
  518.                     if (textlength <= c.getTitle().length()) {
  519.                         if (c.getTitle().toLowerCase().contains(text.toString().toLowerCase())) {
  520.                             tempArrayList.add(c);
  521.                         }
  522.                     }
  523.                 }
  524.                 adapter = new ContentAdapter(getActivity(), tempArrayList);
  525.                 listView.setAdapter(adapter);
  526.                 //Toast.makeText(getActivity().getApplicationContext(), "sssssss "+text, Toast.LENGTH_LONG).show();
  527.                 return false;
  528.             }
  529.         });
  530.         super.onCreateOptionsMenu(menu, menuinflater);
  531.     }
  532.  
  533.     @Override
  534.     public boolean onOptionsItemSelected(MenuItem item) {
  535.         // Handle action bar item clicks here. The action bar will
  536.         // automatically handle clicks on the Home/Up button, so long
  537.         // as you specify a parent activity in AndroidManifest.xml.
  538.         int id = item.getItemId();
  539.  
  540.         //noinspection SimplifiableIfStatement
  541.         if (id == R.id.action_search) {
  542.             return true;
  543.         }
  544.  
  545.         return super.onOptionsItemSelected(item);
  546.     }
  547.  
  548.     public void openBottomSheet (View v, final String title, final String content, String date) {
  549.  
  550.         View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_information, null);
  551.         GridView grid;
  552.         String update;
  553.         final TextView _title, _content, _date;
  554.         _title=(TextView)view.findViewById(R.id.txt_title);
  555.         _content=(TextView)view.findViewById(R.id.txt_detail);
  556.         _date=(TextView)view.findViewById(R.id.txt_date);
  557.         update = content.replace("<p>", "");
  558.         update = update.replace("</p>", "");
  559.         update = update.replace("<strong>", "");
  560.         update = update.replace("</strong>", "");
  561.         _title.setText(title);
  562.         _content.setText(update);
  563.         _date.setText(date);
  564.  
  565.  
  566.         CustomGrid adapter = new CustomGrid(getActivity(), menu, imageId);
  567.         grid=(GridView)view.findViewById(R.id.gridView);
  568.         grid.setAdapter(adapter);
  569.         grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  570.  
  571.             @Override
  572.             public void onItemClick(AdapterView<?> parent, View view,
  573.                                     int position, long id) {
  574.                 //Toast.makeText(getActivity(), "You Clicked at " +menu[+ position], Toast.LENGTH_SHORT).show();
  575.                 switch (position) {
  576.                     case 0:
  577.                         String to = "";
  578.                         String subject = title;
  579.                         String message = content;
  580.  
  581.                         Intent email = new Intent(Intent.ACTION_SEND);
  582.                         email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
  583.                         email.putExtra(Intent.EXTRA_SUBJECT, subject);
  584.                         email.putExtra(Intent.EXTRA_TEXT, message);
  585.                         break;
  586.                     case 1:
  587.                         new PostBookmark().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  588.                         //Toast.makeText(getActivity(),"Bookmark has been added", Toast.LENGTH_SHORT).show();
  589.                         break;
  590.                     case 2:
  591.                         Intent intent;
  592.                         intent = new Intent(getActivity(), AddNoteActivity.class);
  593.                         intent.putExtra("title", title);
  594.                         intent.putExtra("content_", content);
  595.                         intent.putExtra("pointer_", "_");
  596.                         intent.putExtra(EXTRA_DEFAULT_INTENT, 0);
  597.                         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  598.                         startActivity(intent);
  599.                         getActivity().overridePendingTransition(R.anim.pull_in_right, R.anim.push_out_left);
  600.  
  601.                         break;
  602.                     default:
  603.                         break;
  604.                 }
  605.  
  606.             }
  607.         });
  608.  
  609.         final Dialog mBottomSheetDialog = new Dialog(getActivity(),
  610.                 R.style.MaterialDialogSheet);
  611.         mBottomSheetDialog.setContentView(view);
  612.         mBottomSheetDialog.setCancelable(true);
  613.         mBottomSheetDialog.getWindow().setLayout(LinearLayout.LayoutParams.MATCH_PARENT,
  614.                 LinearLayout.LayoutParams.WRAP_CONTENT);
  615.         mBottomSheetDialog.getWindow().setGravity(Gravity.BOTTOM);
  616.         mBottomSheetDialog.show();
  617.     }
  618.  
  619.     //-------------------------------POST BOOKMARK---------------------------------
  620.     private class PostBookmark extends AsyncTask<Void, Void, Void> {
  621.  
  622.         @Override
  623.         protected Void doInBackground(Void... params) {
  624.             String url = "http://jumpbun.com/mosapi/postBookmark";
  625.             String hash = "";
  626.  
  627.  
  628.             BasicHttpParams basichttpparams = new BasicHttpParams();
  629.             HttpConnectionParams.setConnectionTimeout(basichttpparams, 60000);
  630.             HttpConnectionParams.setSoTimeout(basichttpparams, 60000);
  631.  
  632.             DefaultHttpClient defaulthttpclient = new DefaultHttpClient(basichttpparams);
  633.  
  634.             httpclient = defaulthttpclient;
  635.             HttpProtocolParams.setUseExpectContinue(httpclient.getParams(), false);
  636.             BasicCookieStore basiccookiestore = new BasicCookieStore();
  637.  
  638.             localContext = new BasicHttpContext();
  639.             localContext.setAttribute("http.cookie-store", basiccookiestore);
  640.  
  641.             try {
  642.                 hash = URLEncoder.encode(hash, "utf-8");
  643.             } catch (UnsupportedEncodingException e1) {
  644.                 // TODO Auto-generated catch block
  645.                 e1.printStackTrace();
  646.             }
  647.  
  648.             String s = "";
  649.             String message;
  650.  
  651.             HttpPost p = new HttpPost(url);
  652.  
  653.  
  654.             JSONObject object = new JSONObject();
  655.             try {
  656.  
  657.  
  658.                 Log.d("Ini id bookmark", idArticleBookmark);
  659.                 object.put("articleCode", idArticleBookmark);
  660.                 object.put("email", "yogi@code.id");
  661.  
  662.             } catch (JSONException e) {
  663.                 // TODO Auto-generated catch block
  664.                 e.printStackTrace();
  665.             }
  666.             message = object.toString();
  667.             Log.d("INI OBJECT", message);
  668.  
  669.             try {
  670.                 p.setEntity(new StringEntity(message, "UTF8"));
  671.             } catch (UnsupportedEncodingException e1) {
  672.                 // TODO Auto-generated catch block
  673.                 e1.printStackTrace();
  674.             }
  675.             p.setHeader("Content-type", "application/json");
  676.             try {
  677.                 response = httpclient.execute(p, localContext);
  678.                 try {
  679.                     s = EntityUtils.toString(response.getEntity());
  680.                 } catch (IOException e) {
  681.                     // TODO Auto-generated catch block
  682.                     e.printStackTrace();
  683.                 }
  684.  
  685.                 httpclient.getConnectionManager().shutdown();
  686.  
  687.                 Log.d("Response: ", (new StringBuilder("> ")).append(s).toString());
  688.                 JSONObject jsonObj = null;
  689.                 try {
  690.                     JSONObject jObject = new JSONObject(s);
  691.                     bookmarkID = jObject.getString("bookmarkID");
  692.                     Log.d("Ini bookmark id",bookmarkID);
  693.                 }catch (Exception e)
  694.                 {
  695.                     e.printStackTrace();
  696.                 }
  697.                 if (s != null) {
  698.                     try {
  699.                         jsonObj = new JSONObject(s);
  700.                         int code = response.getStatusLine().getStatusCode();
  701.                         if (code == 200) {
  702.  
  703.                             resultfinal = 1;
  704.                             errCode = 0;
  705.                         } else {
  706.                             errCode = 1000;
  707.                             //errMsg = metadata.getString("Message");
  708.                             String errMsgAll = jsonObj.getString("response");
  709.                             String[] arrayErrMsg = errMsgAll.split("\\|");
  710.                             errMsg = arrayErrMsg[1];
  711.                         }
  712.  
  713.                     } catch (JSONException e) {
  714.                         // TODO Auto-generated catch block
  715.                         e.printStackTrace();
  716.                     }
  717.                 }
  718.                 Log.d("Status line", "" + response.getStatusLine().getStatusCode());
  719.             } catch (SocketTimeoutException e) {
  720.                 // handle timeouts
  721.                 e.printStackTrace();
  722.             } catch (ClientProtocolException e) {
  723.                 e.printStackTrace();
  724.             } catch (NullPointerException e) {
  725.                 e.printStackTrace();
  726.             } catch (IOException e) {
  727.                 // TODO Auto-generated catch block
  728.                 e.printStackTrace();
  729.             }
  730.             return null;
  731.         }
  732.  
  733.         protected void onPostExecute(Void paramVoid) {
  734.             super.onPostExecute(paramVoid);
  735.             if (errCode == 1000) {
  736.                 new AlertDialog.Builder(getActivity())
  737.                         .setTitle("Error Message")
  738.                         .setMessage(errMsg)
  739.                         .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
  740.                             public void onClick(DialogInterface dialog, int which) {
  741.                             }
  742.                         })
  743.                         .setCancelable(false)
  744.                         .setIcon(android.R.drawable.ic_dialog_alert)
  745.                         .show();
  746.             } else {
  747.                 if (resultfinal == 1) {
  748.  
  749.                     showAlert("Data Has Been Saved");
  750.                 } else {
  751.                     showAlert("Sorry can't add your not, Please Try Again");
  752.                 }
  753.             }
  754.         }
  755.  
  756.         protected void onPreExecute() {
  757.             super.onPreExecute();
  758.         }
  759.  
  760.  
  761.         private void showAlert(final String message) {
  762.             AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
  763.             builder.setMessage(message).setTitle("Information")
  764.                     .setCancelable(false)
  765.                     .setPositiveButton("OK", new DialogInterface.OnClickListener() {
  766.                         public void onClick(DialogInterface dialog, int id) {
  767.  
  768.                             if (message.equalsIgnoreCase("Data Has Been Saved")) {
  769.                                 final DatabaseHandler db = new DatabaseHandler(getActivity());
  770.                                 String userID = "Test1234";
  771.                                 Log.d("Ini Bookmark", bookmarkID + idArticleBookmark + userID + 1 + getDateTime() + "yogi@code.id" + getDateTime() + "yogi@code.id");
  772.                                 db.addBookmark(new Bookmark(bookmarkID, idArticleBookmark, userID, 1, getDateTime(), "yogi@code.id", getDateTime(), "yogi@code.id"));
  773.  
  774.                                 getActivity().recreate();
  775.                             }
  776.                             // do nothing
  777.                         }
  778.                     });
  779.             AlertDialog alert = builder.create();
  780.             alert.show();
  781.         }
  782.     }
  783.     private String getDateTime() {
  784.         SimpleDateFormat dateFormat = new SimpleDateFormat(
  785.                 "yyyy-MM-dd HH:mm:ss", Locale.getDefault());
  786.         Date date = new Date();
  787.         return dateFormat.format(date);
  788.     }
  789.  
  790.  
  791. }
  792.  
  793. //---------------------------------------------BOOKMARK
  794.  
  795. public class BookmarkFragment extends Fragment {
  796.  
  797.     @Nullable
  798.     @Override
  799.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  800.         return inflater.inflate(R.layout.fragment_bookmark,null);
  801.  
  802.     }
  803.  
  804.     @Override
  805.     public void onViewCreated(View view, Bundle savedInstanceState) {
  806.         super.onViewCreated(view, savedInstanceState);
  807.         Toolbar toolbar = (Toolbar) (this.getActivity()).findViewById(R.id.toolbar);
  808.         toolbar.setTitle("Bookmark");
  809.     }
  810.  
  811.  
  812. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement