am_dot_com

PDM2 20210107

Jan 7th, 2021 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.11 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context=".MainActivity">
  8.  
  9. <TextView
  10. android:gravity="center_horizontal"
  11. app:layout_constraintTop_toTopOf="parent"
  12. android:id="@+id/idTvAbout"
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="@string/strTvAbout"/>
  16.  
  17. <TextView
  18. android:gravity="center_horizontal"
  19. app:layout_constraintTop_toBottomOf="@id/idTvAbout"
  20. android:id="@+id/idTvCapturedText"
  21. android:layout_width="match_parent"
  22. android:layout_height="wrap_content"
  23. />
  24.  
  25. <ImageView
  26. app:layout_constraintTop_toBottomOf="@id/idTvCapturedText"
  27. android:id="@+id/idIvCapturedImage"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"/>
  30.  
  31. </androidx.constraintlayout.widget.ConstraintLayout>
  32.  
  33. //
  34. package com.joythis.android.pdm2textcapturer;
  35.  
  36. import androidx.appcompat.app.AppCompatActivity;
  37.  
  38. import android.content.Context;
  39. import android.content.Intent;
  40. import android.net.Uri;
  41. import android.os.Bundle;
  42. import android.util.Log;
  43. import android.widget.ImageView;
  44. import android.widget.TextView;
  45. import android.widget.Toast;
  46.  
  47. import java.io.File;
  48. import java.io.FileInputStream;
  49. import java.io.FileOutputStream;
  50. import java.io.InputStreamReader;
  51. import java.io.OutputStreamWriter;
  52. import java.nio.charset.StandardCharsets;
  53.  
  54. public class MainActivity extends AppCompatActivity {
  55. Context mContext;
  56. TextView mTvAbout, mTvCapturedText;
  57. ImageView mIvCapturedImage;
  58.  
  59. public final static String MY_DB = "MY_SHARED_TEXTS.DB";
  60.  
  61. @Override
  62. protected void onCreate(Bundle savedInstanceState) {
  63. super.onCreate(savedInstanceState);
  64. setContentView(R.layout.activity_main);
  65.  
  66. init();
  67. }//onCreate
  68.  
  69. void init(){
  70. mContext = this;
  71.  
  72. mTvAbout = findViewById(R.id.idTvAbout);
  73. mTvCapturedText = findViewById (R.id.idTvCapturedText);
  74. mIvCapturedImage = findViewById (R.id.idIvCapturedImage);
  75.  
  76. checkIfCalledByAnotherAppAndReceiveItsSharedData();
  77.  
  78. mTvAbout.setText(
  79. whereIsThePrivateInternalStorage()
  80. +
  81. "\n"
  82. +
  83. getString(R.string.strTvAbout)
  84. );
  85. }//init
  86.  
  87. void checkIfCalledByAnotherAppAndReceiveItsSharedData(){
  88. Intent intentHowWasICalled = getIntent();
  89.  
  90. if (intentHowWasICalled!=null){
  91. String strAction = intentHowWasICalled.getAction();
  92.  
  93. boolean bItWasTheUserInterfaceOrAndroidStudio =
  94. strAction.equals(Intent.ACTION_MAIN);
  95.  
  96. boolean bIsItActionSend =
  97. strAction.equals(Intent.ACTION_SEND); //share!
  98.  
  99. //is this a share situation?
  100. if (bIsItActionSend){
  101. String strType =
  102. intentHowWasICalled.getType(); //text/html text/plain
  103.  
  104. //curious (serves both situations: text and image)
  105. Toast.makeText(
  106. mContext,
  107. strType,
  108. Toast.LENGTH_LONG
  109. ).show();
  110.  
  111. boolean bIsItSharedText =
  112. strType.startsWith("text/");
  113.  
  114. boolean bIsItSharedImage =
  115. strType.startsWith("image/");
  116.  
  117. if (bIsItSharedText){
  118. //receive the shared text
  119. String strSharedText =
  120. intentHowWasICalled.getStringExtra(
  121. Intent.EXTRA_TEXT
  122. );
  123.  
  124. //display the received text in mTvCapturedText
  125. mTvCapturedText.setText(strSharedText);
  126.  
  127. insert(strSharedText);
  128. }//if
  129.  
  130. if (bIsItSharedImage){
  131. //TODO: how to handle image sharing?
  132. Uri uriForTheImage =
  133. intentHowWasICalled.getParcelableExtra(
  134. Intent.EXTRA_STREAM
  135. );
  136. mIvCapturedImage.setImageURI(uriForTheImage);
  137. }
  138. }//if
  139. }//if
  140. }//checkIfCalledByAnotherAppAndReceiveItsSharedData
  141.  
  142. Boolean insert(String pInsertWhat){
  143. return writeContentToFileInPIS(
  144. MY_DB,
  145. pInsertWhat+"\n", //TODO! "when \t what \n"
  146. MODE_APPEND
  147. );
  148. }//Boolean
  149.  
  150. //---------------------------------------
  151. /*
  152. private internal storage?
  153. non-volatile memory region in the file system
  154. exclusive to each app
  155. */
  156.  
  157. String whereIsThePrivateInternalStorage(){
  158. File pis = this.getFilesDir();
  159. return pis.getAbsolutePath();
  160. }//whereIsThePrivateInternalStorage
  161.  
  162. Boolean writeContentToFileInPIS(
  163. String pFileName,
  164. String pFileContent,
  165. int pWriteMode //MODE_PRIVATE , MODE_APPEND
  166. ){
  167. try{
  168. FileOutputStream fos =
  169. this.openFileOutput(
  170. pFileName,
  171. pWriteMode
  172. );
  173.  
  174. if (fos!=null){
  175. //fos.write(pFileContent);
  176. OutputStreamWriter osw =
  177. new OutputStreamWriter(
  178. fos,
  179. StandardCharsets.UTF_8
  180. );
  181.  
  182. if (osw!=null){
  183. osw.write(pFileContent);
  184.  
  185. osw.close();
  186. fos.close();
  187. return true;
  188. }//if got OutputStreamWrite
  189. }//if got FileOutputStream
  190. }//try
  191. catch (Exception e){
  192. Log.e(
  193. getClass().getName(),
  194. e.toString()
  195. );
  196. }//catch
  197.  
  198. return false;
  199. }//writeContentToFileInPIS
  200.  
  201. String readContentFromFileInPIS (
  202. String pFileName
  203. ){
  204. try {
  205. FileInputStream fis = this.openFileInput(pFileName);
  206. if (fis!=null){
  207. InputStreamReader isr = new InputStreamReader(
  208. fis,
  209. StandardCharsets.UTF_8
  210. );
  211. if (isr!=null){
  212. char c; int i; String strAll = "";
  213.  
  214. while((i=isr.read())!=-1){
  215. c = (char)i;
  216. strAll+=c;
  217. }//while
  218.  
  219. isr.close();
  220. fis.close();
  221.  
  222. return strAll;
  223. }//if
  224. }//if
  225. }//try
  226. catch (Exception e){
  227. Log.e(
  228. getClass().getName(),
  229. e.toString()
  230. );
  231. }//catch
  232.  
  233. return "";
  234. }//readContentFromFileInPIS
  235.  
  236. }//MainActivity
Add Comment
Please, Sign In to add comment