Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.joythis.android.textcapturer;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.core.widget.TintableImageSourceView;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.View;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.Calendar;
- import java.util.HashMap;
- import java.util.Map;
- public class MainActivity extends AppCompatActivity {
- Context mContext;
- TextView mTvAbout, mTvCapturedText;
- ArrayList<String> mAlRecords;
- ArrayAdapter<String> mAd;
- ListView mLvRecords;
- public final static String MY_TSV_DB = "SHARES.DB";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- init();
- }//onCreate
- boolean qualityControl(
- Object[] pCheckThese
- ){
- boolean bRet = true;
- for (Object o : pCheckThese){
- bRet = bRet && (o!=null);
- }//for
- return bRet;
- }//qualityControl
- Map<Object, Boolean> altQualityControl(
- Object[] pCheckThese
- ){
- Map<Object, Boolean> aRet= new HashMap<>();
- for (Object o : pCheckThese){
- boolean bOk = o!=null;
- aRet.put(o, bOk);
- }//for
- return aRet;
- }//altQualityControl
- void init(){
- mContext = this;
- mTvAbout = findViewById(R.id.idTvAbout);
- mTvCapturedText = findViewById(R.id.idTvCapturedText);
- mLvRecords = findViewById(R.id.idLvRecords);
- mAlRecords = new ArrayList<>();
- mAd = new ArrayAdapter<>(
- mContext,
- android.R.layout.simple_list_item_1,
- mAlRecords
- );
- mLvRecords.setAdapter(mAd);
- Object[] aRelevant = {mTvAbout, mTvCapturedText};
- boolean bCanProceed = qualityControl(aRelevant);
- if (!bCanProceed){
- Toast t = Toast.makeText(
- this,
- getString(R.string.strThereIsNullObject),
- Toast.LENGTH_LONG
- );
- return;
- }//if
- mTvAbout.setText(
- whereIsThePrivateInternalStorage()
- +"\nText Captured:"
- );
- checkIfCalledForSharingAndDisplaySharedText();
- refresh();
- }//init
- void checkIfCalledForSharingAndDisplaySharedText(){
- Intent intentHowWasICalled = getIntent();
- if (intentHowWasICalled!=null){
- String strAction =
- intentHowWasICalled.getAction();
- boolean bIWantToAnswer =
- strAction.equals(Intent.ACTION_SEND);
- boolean bIWantToAnswer2 =
- strAction.compareTo(Intent.ACTION_SEND)==0;
- boolean bIWantToAnswer3 =
- strAction.compareToIgnoreCase(Intent.ACTION_SEND)==0;
- if (bIWantToAnswer){
- String strType =
- intentHowWasICalled.getType();
- boolean bIsText =
- strType.startsWith("text/");
- boolean bIsImage =
- strType.startsWith("image/");
- if (bIsText){
- String strTextThatWasShared =
- intentHowWasICalled.getStringExtra(
- Intent.EXTRA_TEXT
- );
- insert(strTextThatWasShared);
- refresh();
- mTvCapturedText.setVisibility(View.GONE);
- mTvCapturedText.setText(
- strTextThatWasShared
- );
- }//if text was shared
- }//if the Activity was called from an ACTION_SEND Intent (e.g. Share from another app)
- }//if got an Intent object with all the necessary data
- }//checkIfCalledForSharingAndDisplaySharedText
- void refresh(){
- /*
- String strNewContent = readFile(MY_TSV_DB);
- mTvCapturedText.setText(strNewContent);
- */
- mAlRecords.clear();
- //mAlRecords.addAll(readFile(MY_TSV_DB)); //original order
- ArrayList<String> alTemp = readFile(MY_TSV_DB);
- for (int idx=0; idx<alTemp.size(); idx++){
- String strContent = alTemp.get(idx);
- mAlRecords.add(0, strContent);
- }//for
- mAd.notifyDataSetChanged();
- }//refresh
- /*String*/ArrayList<String> readFile(
- String pFileName
- ){
- String strContent =
- readFromPrivateInternalStorage(pFileName);
- /*
- somewhat useless while the app is not rewritten using
- a true "Record" class data type
- and a collection of Records is kept (for example in ArrayList<Record>)
- */
- ArrayList<String> aRet = new ArrayList<>();
- String[] aRecords =
- strContent.split("\n");
- for(String record : aRecords){
- String strAfterReplacement =
- record.replace(
- "<br>", "\n"
- );
- aRet.add(strAfterReplacement);
- }//for
- return aRet;
- //return strContent;
- }//readFile
- void insert(String pStrWhatToInsert){
- Calendar cal = Calendar.getInstance();
- String strYMD = String.format(
- "%d-%d-%d",
- cal.get(Calendar.YEAR),
- (cal.get(Calendar.MONTH)+1),
- cal.get(Calendar.DAY_OF_MONTH)
- );
- String strAfterReplacement =
- pStrWhatToInsert.replace(
- "\n",
- "<br>"
- );
- String strRecord = String.format(
- "%s\t%s",
- strYMD,
- strAfterReplacement+"\n"
- );
- Boolean bOK =
- writeToPrivateInternalStorage(
- MY_TSV_DB,
- //pStrWhatToInsert+"\n",
- strRecord,
- MODE_APPEND
- );
- if (bOK){
- Toast t = Toast.makeText(
- mContext,
- "New record added to TSV DB",
- Toast.LENGTH_LONG
- );
- t.show();
- }//if
- }//insert
- /*
- to read and write in the Private Internal Storage
- we need tools for dumping and recovering text from
- that exclusive non-volatile file-system area
- */
- String whereIsThePrivateInternalStorage(){
- File pis = this.getFilesDir();
- return pis.getAbsolutePath();
- }//whereIsThePrivateInternalStorage
- Boolean writeToPrivateInternalStorage(
- String pFileName,
- String pFileContent,
- int pWriteMode //PRIVATE (destructive) / APPEND (cumulative)
- ){
- try {
- FileOutputStream fos = openFileOutput(
- pFileName,
- //MODE_APPEND
- pWriteMode
- );
- if (fos!=null){
- OutputStreamWriter osw = new OutputStreamWriter(
- fos,
- StandardCharsets.UTF_8
- );
- if (osw!=null){
- osw.write(pFileContent);
- osw.close();
- fos.close();
- return true;
- }//if
- }//if
- }//try
- catch (Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- return false;
- }//writeToPrivateInternalStorage
- String readFromPrivateInternalStorage(
- String pFileName
- ){
- try{
- FileInputStream fis = this.openFileInput(pFileName);
- if (fis!=null){
- InputStreamReader isr = new InputStreamReader(
- fis,
- StandardCharsets.UTF_8
- );
- if (isr!=null){
- int i; char c; String strAll="";
- while ((i=isr.read())!=-1){
- c = (char) i;
- strAll+=c;
- }//while
- isr.close();
- fis.close();
- return strAll;
- }//if
- }//if
- }//try
- catch(Exception e){
- Log.e(
- getClass().getName(),
- e.toString()
- );
- }//catch
- return "";
- }//readFromPrivateInternalStorage
- }//MainActivity
- //
- <?xml version="1.0" encoding="utf-8"?>
- <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context=".MainActivity">
- <!-- Text captured: -->
- <TextView
- android:gravity="center_horizontal"
- android:text="@string/strTvAbout"
- android:id="@+id/idTvAbout"
- app:layout_constraintTop_toTopOf="parent"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"></TextView>
- <!-- the exact text that was shared from an external app will
- show here -->
- <TextView
- android:id="@+id/idTvCapturedText"
- app:layout_constraintLeft_toLeftOf="parent"
- app:layout_constraintTop_toBottomOf="@id/idTvAbout"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"/>
- <ListView
- android:id="@+id/idLvRecords"
- app:layout_constraintTop_toBottomOf="@id/idTvCapturedText"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"></ListView>
- </androidx.constraintlayout.widget.ConstraintLayout>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement