Advertisement
am_dot_com

DDM 20210105

Jan 5th, 2021 (edited)
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 10.09 KB | None | 0 0
  1. package com.joythis.android.textcapturer;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4. import androidx.core.widget.TintableImageSourceView;
  5.  
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.os.Bundle;
  9. import android.util.Log;
  10. import android.view.View;
  11. import android.widget.ArrayAdapter;
  12. import android.widget.ListView;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileOutputStream;
  19. import java.io.InputStreamReader;
  20. import java.io.OutputStreamWriter;
  21. import java.nio.charset.StandardCharsets;
  22. import java.util.ArrayList;
  23. import java.util.Calendar;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26.  
  27. public class MainActivity extends AppCompatActivity {
  28.     Context mContext;
  29.     TextView mTvAbout, mTvCapturedText;
  30.  
  31.     ArrayList<String> mAlRecords;
  32.     ArrayAdapter<String> mAd;
  33.     ListView mLvRecords;
  34.  
  35.     public final static String MY_TSV_DB = "SHARES.DB";
  36.  
  37.     @Override
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.         super.onCreate(savedInstanceState);
  40.         setContentView(R.layout.activity_main);
  41.  
  42.         init();
  43.     }//onCreate
  44.  
  45.     boolean qualityControl(
  46.         Object[] pCheckThese
  47.     ){
  48.         boolean bRet = true;
  49.         for (Object o : pCheckThese){
  50.             bRet = bRet && (o!=null);
  51.         }//for
  52.  
  53.         return bRet;
  54.     }//qualityControl
  55.  
  56.     Map<Object, Boolean> altQualityControl(
  57.         Object[] pCheckThese
  58.     ){
  59.         Map<Object, Boolean> aRet= new HashMap<>();
  60.         for (Object o : pCheckThese){
  61.             boolean bOk = o!=null;
  62.             aRet.put(o, bOk);
  63.         }//for
  64.         return aRet;
  65.     }//altQualityControl
  66.  
  67.     void init(){
  68.         mContext = this;
  69.         mTvAbout = findViewById(R.id.idTvAbout);
  70.         mTvCapturedText = findViewById(R.id.idTvCapturedText);
  71.  
  72.         mLvRecords = findViewById(R.id.idLvRecords);
  73.         mAlRecords = new ArrayList<>();
  74.         mAd = new ArrayAdapter<>(
  75.             mContext,
  76.             android.R.layout.simple_list_item_1,
  77.             mAlRecords
  78.         );
  79.         mLvRecords.setAdapter(mAd);
  80.  
  81.         Object[] aRelevant = {mTvAbout, mTvCapturedText};
  82.         boolean bCanProceed = qualityControl(aRelevant);
  83.  
  84.         if (!bCanProceed){
  85.             Toast t = Toast.makeText(
  86.                 this,
  87.                 getString(R.string.strThereIsNullObject),
  88.                 Toast.LENGTH_LONG
  89.             );
  90.             return;
  91.         }//if
  92.  
  93.         mTvAbout.setText(
  94.             whereIsThePrivateInternalStorage()
  95.                 +"\nText Captured:"
  96.         );
  97.         checkIfCalledForSharingAndDisplaySharedText();
  98.  
  99.         refresh();
  100.     }//init
  101.  
  102.     void checkIfCalledForSharingAndDisplaySharedText(){
  103.         Intent intentHowWasICalled = getIntent();
  104.  
  105.         if (intentHowWasICalled!=null){
  106.             String strAction =
  107.                 intentHowWasICalled.getAction();
  108.  
  109.             boolean bIWantToAnswer =
  110.             strAction.equals(Intent.ACTION_SEND);
  111.  
  112.             boolean bIWantToAnswer2 =
  113.             strAction.compareTo(Intent.ACTION_SEND)==0;
  114.  
  115.             boolean bIWantToAnswer3 =
  116.             strAction.compareToIgnoreCase(Intent.ACTION_SEND)==0;
  117.  
  118.             if (bIWantToAnswer){
  119.                 String strType =
  120.                     intentHowWasICalled.getType();
  121.  
  122.                 boolean bIsText =
  123.                     strType.startsWith("text/");
  124.  
  125.                 boolean bIsImage =
  126.                     strType.startsWith("image/");
  127.  
  128.                 if (bIsText){
  129.                     String strTextThatWasShared =
  130.                     intentHowWasICalled.getStringExtra(
  131.                         Intent.EXTRA_TEXT
  132.                     );
  133.  
  134.                     insert(strTextThatWasShared);
  135.                     refresh();
  136.  
  137.                     mTvCapturedText.setVisibility(View.GONE);
  138.                     mTvCapturedText.setText(
  139.                         strTextThatWasShared
  140.                     );
  141.                 }//if text was shared
  142.             }//if the Activity was called from an ACTION_SEND Intent (e.g. Share from another app)
  143.         }//if got an Intent object with all the necessary data
  144.     }//checkIfCalledForSharingAndDisplaySharedText
  145.  
  146.     void refresh(){
  147.         /*
  148.         String strNewContent = readFile(MY_TSV_DB);
  149.         mTvCapturedText.setText(strNewContent);
  150.          */
  151.         mAlRecords.clear();
  152.         //mAlRecords.addAll(readFile(MY_TSV_DB)); //original order
  153.         ArrayList<String> alTemp = readFile(MY_TSV_DB);
  154.         for (int idx=0; idx<alTemp.size(); idx++){
  155.            String strContent = alTemp.get(idx);
  156.            mAlRecords.add(0, strContent);
  157.        }//for
  158.  
  159.        mAd.notifyDataSetChanged();
  160.    }//refresh
  161.  
  162.    /*String*/ArrayList<String> readFile(
  163.         String pFileName
  164.     ){
  165.         String strContent =
  166.         readFromPrivateInternalStorage(pFileName);
  167.  
  168.         /*
  169.         somewhat useless while the app is not rewritten using
  170.         a true "Record" class data type
  171.         and a collection of Records is kept (for example in ArrayList<Record>)
  172.          */
  173.         ArrayList<String> aRet = new ArrayList<>();
  174.         String[] aRecords =
  175.             strContent.split("\n");
  176.         for(String record : aRecords){
  177.             String strAfterReplacement =
  178.             record.replace(
  179.                 "<br>", "\n"
  180.             );
  181.             aRet.add(strAfterReplacement);
  182.         }//for
  183.         return aRet;
  184.  
  185.         //return strContent;
  186.     }//readFile
  187.  
  188.     void insert(String pStrWhatToInsert){
  189.         Calendar cal = Calendar.getInstance();
  190.         String strYMD = String.format(
  191.             "%d-%d-%d",
  192.             cal.get(Calendar.YEAR),
  193.             (cal.get(Calendar.MONTH)+1),
  194.             cal.get(Calendar.DAY_OF_MONTH)
  195.         );
  196.  
  197.         String strAfterReplacement =
  198.             pStrWhatToInsert.replace(
  199.                 "\n",
  200.             "<br>"
  201.             );
  202.  
  203.         String strRecord = String.format(
  204.             "%s\t%s",
  205.             strYMD,
  206.             strAfterReplacement+"\n"
  207.         );
  208.  
  209.         Boolean bOK =
  210.             writeToPrivateInternalStorage(
  211.                 MY_TSV_DB,
  212.                 //pStrWhatToInsert+"\n",
  213.                     strRecord,
  214.                 MODE_APPEND
  215.             );
  216.  
  217.         if (bOK){
  218.             Toast t = Toast.makeText(
  219.                 mContext,
  220.                 "New record added to TSV DB",
  221.                 Toast.LENGTH_LONG
  222.             );
  223.             t.show();
  224.         }//if
  225.     }//insert
  226.  
  227.     /*
  228.     to read and write in the Private Internal Storage
  229.     we need tools for dumping and recovering text from
  230.     that exclusive non-volatile file-system area
  231.      */
  232.  
  233.     String whereIsThePrivateInternalStorage(){
  234.         File pis = this.getFilesDir();
  235.         return pis.getAbsolutePath();
  236.     }//whereIsThePrivateInternalStorage
  237.  
  238.     Boolean writeToPrivateInternalStorage(
  239.         String pFileName,
  240.         String pFileContent,
  241.         int pWriteMode //PRIVATE (destructive) / APPEND (cumulative)
  242.     ){
  243.         try {
  244.             FileOutputStream fos = openFileOutput(
  245.                 pFileName,
  246.                 //MODE_APPEND
  247.                 pWriteMode
  248.             );
  249.  
  250.             if (fos!=null){
  251.                 OutputStreamWriter osw = new OutputStreamWriter(
  252.                     fos,
  253.                     StandardCharsets.UTF_8
  254.                 );
  255.                 if (osw!=null){
  256.                     osw.write(pFileContent);
  257.                     osw.close();
  258.                     fos.close();
  259.                     return true;
  260.                 }//if
  261.             }//if
  262.         }//try
  263.         catch (Exception e){
  264.             Log.e(
  265.                 getClass().getName(),
  266.                 e.toString()
  267.             );
  268.         }//catch
  269.         return false;
  270.     }//writeToPrivateInternalStorage
  271.  
  272.     String readFromPrivateInternalStorage(
  273.         String pFileName
  274.     ){
  275.         try{
  276.             FileInputStream fis = this.openFileInput(pFileName);
  277.             if (fis!=null){
  278.                 InputStreamReader isr = new InputStreamReader(
  279.                     fis,
  280.                     StandardCharsets.UTF_8
  281.                 );
  282.  
  283.                 if (isr!=null){
  284.                     int i; char c; String strAll="";
  285.                     while ((i=isr.read())!=-1){
  286.                         c = (char) i;
  287.                         strAll+=c;
  288.                     }//while
  289.                     isr.close();
  290.                     fis.close();
  291.  
  292.                     return strAll;
  293.                 }//if
  294.             }//if
  295.         }//try
  296.         catch(Exception e){
  297.             Log.e(
  298.                 getClass().getName(),
  299.                 e.toString()
  300.             );
  301.         }//catch
  302.  
  303.         return "";
  304.     }//readFromPrivateInternalStorage
  305. }//MainActivity
  306.  
  307. //
  308.  
  309. <?xml version="1.0" encoding="utf-8"?>
  310. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  311.    xmlns:app="http://schemas.android.com/apk/res-auto"
  312.    xmlns:tools="http://schemas.android.com/tools"
  313.    android:layout_width="match_parent"
  314.    android:layout_height="match_parent"
  315.    tools:context=".MainActivity">
  316.  
  317.     <!-- Text captured: -->
  318.     <TextView
  319.        android:gravity="center_horizontal"
  320.        android:text="@string/strTvAbout"
  321.        android:id="@+id/idTvAbout"
  322.        app:layout_constraintTop_toTopOf="parent"
  323.        android:layout_width="match_parent"
  324.        android:layout_height="wrap_content"></TextView>
  325.  
  326.     <!-- the exact text that was shared from an external app will
  327.    show here -->
  328.  
  329.     <TextView
  330.        android:id="@+id/idTvCapturedText"
  331.        app:layout_constraintLeft_toLeftOf="parent"
  332.        app:layout_constraintTop_toBottomOf="@id/idTvAbout"
  333.        android:layout_width="wrap_content"
  334.        android:layout_height="wrap_content"/>
  335.  
  336.     <ListView
  337.        android:id="@+id/idLvRecords"
  338.        app:layout_constraintTop_toBottomOf="@id/idTvCapturedText"
  339.        android:layout_width="match_parent"
  340.        android:layout_height="wrap_content"></ListView>
  341.  
  342. </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement