daily pastebin goal
41%
SHARE
TWEET

Untitled

a guest Oct 12th, 2017 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class SMSActivity extends AppCompatActivity {
  2.  
  3. RecyclerView smsRecyclerview;
  4. EditText Smsplace;
  5. RecyclerView.LayoutManager smsLayoutmanager;
  6. SMSAdapter smsAdapter;
  7. ArrayList<SMS> arrayList = new ArrayList<>();
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10.     super.onCreate(savedInstanceState);
  11.     setContentView(R.layout.activity_sms);
  12.  
  13.     smsRecyclerview = (RecyclerView)findViewById(R.id.smsRecyclerView);
  14.     Smsplace = (EditText)findViewById(R.id.smsplace);
  15.     smsLayoutmanager = new LinearLayoutManager(this);
  16.     smsRecyclerview.setLayoutManager(smsLayoutmanager);
  17.     smsRecyclerview.setHasFixedSize(true);
  18.     smsAdapter = new SMSAdapter(arrayList);
  19.     smsRecyclerview.setAdapter(smsAdapter);
  20.     readFromLocalStorage();
  21.  
  22. }
  23.  
  24. public void submitPlace(View view)
  25. {
  26.     String smsplace = Smsplace.getText().toString();
  27.     saveToAppServer(smsplace);
  28.     Smsplace.setText("");
  29. }
  30.  
  31. private void readFromLocalStorage()
  32. {
  33.     arrayList.clear();
  34.     SMSHelper smsHelper = new SMSHelper(this);
  35.     SQLiteDatabase database = smsHelper.getReadableDatabase();
  36.  
  37.     Cursor cursor = smsHelper.readFromLocalDatabase(database);
  38.  
  39.     while (cursor.moveToNext())
  40.     {
  41.         String smsplace = cursor.getString(cursor.getColumnIndex(SMSContract.SMSPLACE));
  42.         int smssync_status = cursor.getInt(cursor.getColumnIndex(SMSContract.SMSSYNC_STATUS));
  43.         arrayList.add(new SMS(smsplace,smssync_status));
  44.     }
  45.  
  46.     smsAdapter.notifyDataSetChanged();
  47.     cursor.close();
  48.  
  49. }
  50.  
  51. private void saveToAppServer(final String smsplace)
  52. {
  53.  
  54.  
  55.     if(checkNetworkConnection())
  56.     {
  57.         StringRequest stringRequest = new StringRequest(Request.Method.POST, SMSContract.SERVER_URL,
  58.                 new Response.Listener<String>() {
  59.                     @Override
  60.                     public void onResponse(String response) {
  61.  
  62.                         try {
  63.                             JSONObject jsonObject = new JSONObject(response);
  64.                             String Response = jsonObject.getString("response");
  65.                             if(Response.equals("OK"))
  66.                             {
  67.                                 saveToLocalStorage(smsplace,SMSContract.SMSSYNC_STATUS_OK);
  68.                             }
  69.                             else
  70.                             {
  71.                                 saveToLocalStorage(smsplace,SMSContract.SMSSYNC_STATUS_FAILED);
  72.                             }
  73.  
  74.                         } catch (JSONException e) {
  75.                             e.printStackTrace();
  76.                         }
  77.  
  78.                     }
  79.                 }, new Response.ErrorListener() {
  80.             @Override
  81.             public void onErrorResponse(VolleyError error) {
  82.  
  83.                 saveToLocalStorage(smsplace,SMSContract.SMSSYNC_STATUS_FAILED);
  84.  
  85.             }
  86.         })
  87.         {
  88.             @Override
  89.             protected Map<String, String> getParams() throws AuthFailureError {
  90.                 Map<String,String> params = new HashMap<>();
  91.                 params.put("smsplace",smsplace);
  92.                 return params;
  93.             }
  94.         }
  95.         ;
  96.         SMSSingleton.getInstance(SMSActivity.this).addToRequestQue(stringRequest);
  97.     }
  98.     else
  99.     {
  100.         saveToLocalStorage(smsplace,SMSContract.SMSSYNC_STATUS_FAILED);
  101.     }
  102.  
  103.  
  104. }
  105.  
  106. public boolean checkNetworkConnection()
  107. {
  108.     ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
  109.     NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  110.     return (networkInfo != null && networkInfo.isConnected());
  111. }
  112.  
  113. private void saveToLocalStorage(String smsplace,int smssync)
  114. {
  115.     SMSHelper smsHelper = new SMSHelper(this);
  116.     SQLiteDatabase database = smsHelper.getWritableDatabase();
  117.     smsHelper.saveToLocalDatabase(smsplace,smssync,database);
  118.     readFromLocalStorage();
  119. }
  120.  
  121. }
  122.    
  123. public class SMSAdapter extends RecyclerView.Adapter<SMSAdapter.MyViewHolder> {
  124.  
  125. private ArrayList<SMS> arrayList = new ArrayList<>();
  126.  
  127.  
  128. public SMSAdapter(ArrayList<SMS> arrayList)
  129. {
  130.     this.arrayList = arrayList;
  131. }
  132.  
  133.  
  134. @Override
  135. public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  136.     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.smsitem_view,parent,false);
  137.     return new MyViewHolder(view);
  138. }
  139.  
  140. @Override
  141. public void onBindViewHolder(MyViewHolder holder, int position) {
  142.  
  143.     holder.Smsplace.setText(arrayList.get(position).getSmsplace());
  144.     int smssync_status = arrayList.get(position).getSmsSync_status();
  145.     if(smssync_status == SMSContract.SMSSYNC_STATUS_OK)
  146.     {
  147.         holder.Smssync_status.setImageResource(R.drawable.ok);
  148.     }
  149.     else
  150.     {
  151.         holder.Smssync_status.setImageResource(R.drawable.sync);
  152.     }
  153.  
  154. }
  155.  
  156. @Override
  157. public int getItemCount() {
  158.     return arrayList.size();
  159. }
  160.  
  161. public static class MyViewHolder extends RecyclerView.ViewHolder
  162. {
  163.  
  164.     ImageView Smssync_status;
  165.     TextView Smsplace;
  166.  
  167.     public MyViewHolder(View itemView) {
  168.         super(itemView);
  169.         Smssync_status = (ImageView)itemView.findViewById(R.id.smsImgsync);
  170.         Smsplace = (TextView)itemView.findViewById(R.id.txtSmsplace);
  171.     }
  172. }
  173.  
  174. }
RAW Paste Data
Top