|
| 1 | +package com.termux.app.activities; |
| 2 | + |
| 3 | +import androidx.annotation.NonNull; |
| 4 | +import androidx.appcompat.app.ActionBar; |
| 5 | +import androidx.appcompat.app.AppCompatActivity; |
| 6 | +import androidx.appcompat.widget.Toolbar; |
| 7 | +import androidx.recyclerview.widget.LinearLayoutManager; |
| 8 | +import androidx.recyclerview.widget.RecyclerView; |
| 9 | + |
| 10 | +import android.content.Context; |
| 11 | +import android.content.Intent; |
| 12 | +import android.os.Bundle; |
| 13 | +import android.view.Menu; |
| 14 | +import android.view.MenuInflater; |
| 15 | +import android.view.MenuItem; |
| 16 | + |
| 17 | +import com.termux.R; |
| 18 | +import com.termux.app.TermuxConstants; |
| 19 | +import com.termux.app.utils.MarkdownUtils; |
| 20 | +import com.termux.app.utils.ShareUtils; |
| 21 | +import com.termux.app.utils.TermuxUtils; |
| 22 | +import com.termux.models.ReportInfo; |
| 23 | + |
| 24 | +import org.commonmark.node.FencedCodeBlock; |
| 25 | + |
| 26 | +import io.noties.markwon.Markwon; |
| 27 | +import io.noties.markwon.recycler.MarkwonAdapter; |
| 28 | +import io.noties.markwon.recycler.SimpleEntry; |
| 29 | + |
| 30 | +public class ReportActivity extends AppCompatActivity { |
| 31 | + |
| 32 | + private static final String ARG_REPORT_INFO = "report_info"; |
| 33 | + |
| 34 | + ReportInfo mReportInfo; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected void onCreate(Bundle savedInstanceState) { |
| 38 | + super.onCreate(savedInstanceState); |
| 39 | + setContentView(R.layout.activity_report); |
| 40 | + |
| 41 | + Toolbar toolbar = findViewById(R.id.toolbar); |
| 42 | + if (toolbar != null) { |
| 43 | + setSupportActionBar(toolbar); |
| 44 | + } |
| 45 | + |
| 46 | + Bundle bundle = null; |
| 47 | + Intent intent = getIntent(); |
| 48 | + if(intent != null) |
| 49 | + bundle = intent.getExtras(); |
| 50 | + else if(savedInstanceState != null) |
| 51 | + bundle = savedInstanceState; |
| 52 | + |
| 53 | + updateUI(bundle); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + protected void onNewIntent(Intent intent) { |
| 59 | + super.onNewIntent(intent); |
| 60 | + setIntent(intent); |
| 61 | + |
| 62 | + if(intent != null) |
| 63 | + updateUI(intent.getExtras()); |
| 64 | + } |
| 65 | + |
| 66 | + private void updateUI(Bundle bundle) { |
| 67 | + |
| 68 | + if (bundle == null) { |
| 69 | + finish(); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + mReportInfo = (ReportInfo) bundle.getSerializable(ARG_REPORT_INFO); |
| 74 | + |
| 75 | + if (mReportInfo == null) { |
| 76 | + finish(); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + final ActionBar actionBar = getSupportActionBar(); |
| 82 | + if (actionBar != null) { |
| 83 | + if (mReportInfo.reportTitle != null) |
| 84 | + actionBar.setTitle(mReportInfo.reportTitle); |
| 85 | + else |
| 86 | + actionBar.setTitle(TermuxConstants.TERMUX_APP_NAME + " App Report"); |
| 87 | + } |
| 88 | + |
| 89 | + |
| 90 | + RecyclerView recyclerView = findViewById(R.id.recycler_view); |
| 91 | + |
| 92 | + final Markwon markwon = MarkdownUtils.getRecyclerMarkwonBuilder(this); |
| 93 | + |
| 94 | + final MarkwonAdapter adapter = MarkwonAdapter.builderTextViewIsRoot(R.layout.activity_report_adapter_node_default) |
| 95 | + .include(FencedCodeBlock.class, SimpleEntry.create(R.layout.activity_report_adapter_node_code_block, R.id.text_view)) |
| 96 | + .build(); |
| 97 | + |
| 98 | + recyclerView.setLayoutManager(new LinearLayoutManager(this)); |
| 99 | + recyclerView.setAdapter(adapter); |
| 100 | + |
| 101 | + adapter.setMarkdown(markwon, mReportInfo.reportString + getReportAndDeviceDetailsMarkdownString()); |
| 102 | + adapter.notifyDataSetChanged(); |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + @Override |
| 108 | + public void onSaveInstanceState(@NonNull Bundle outState) { |
| 109 | + super.onSaveInstanceState(outState); |
| 110 | + |
| 111 | + outState.putSerializable(ARG_REPORT_INFO, mReportInfo); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean onCreateOptionsMenu(final Menu menu) { |
| 116 | + final MenuInflater inflater = getMenuInflater(); |
| 117 | + inflater.inflate(R.menu.menu_report, menu); |
| 118 | + return true; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public void onBackPressed() { |
| 123 | + // Remove activity from recents menu on back button press |
| 124 | + finishAndRemoveTask(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public boolean onOptionsItemSelected(final MenuItem item) { |
| 129 | + int id = item.getItemId(); |
| 130 | + if (id == R.id.menu_item_share_report) { |
| 131 | + if (mReportInfo != null) |
| 132 | + ShareUtils.shareText(this, getString(R.string.report_text), mReportInfo.reportString); |
| 133 | + } else if (id == R.id.menu_item_copy_report) { |
| 134 | + if (mReportInfo != null) |
| 135 | + ShareUtils.copyTextToClipboard(this, mReportInfo.reportString, null); |
| 136 | + } |
| 137 | + |
| 138 | + return false; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Get a markdown {@link String} for {@link #mReportInfo} and device details. |
| 143 | + * |
| 144 | + * @return Returns the markdown {@link String}. |
| 145 | + */ |
| 146 | + private String getReportAndDeviceDetailsMarkdownString() { |
| 147 | + if(!mReportInfo.addReportAndDeviceDetails) return ""; |
| 148 | + |
| 149 | + StringBuilder markdownString = new StringBuilder(); |
| 150 | + |
| 151 | + markdownString.append("\n\n### Report And Device Details\n\n"); |
| 152 | + |
| 153 | + if (mReportInfo != null) { |
| 154 | + markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("User Action", mReportInfo.userAction, "-")); |
| 155 | + markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Sender", mReportInfo.sender, "-")); |
| 156 | + } |
| 157 | + |
| 158 | + markdownString.append("\n").append(MarkdownUtils.getSingleLineMarkdownStringEntry("Timestamp", TermuxUtils.getCurrentTimeStamp(), "-")); |
| 159 | + |
| 160 | + markdownString.append("\n\n").append(TermuxUtils.getDeviceDetailsMarkdownString(this)); |
| 161 | + |
| 162 | + markdownString.append("\n##\n"); |
| 163 | + |
| 164 | + return markdownString.toString(); |
| 165 | + } |
| 166 | + |
| 167 | + |
| 168 | + |
| 169 | + public static void startReportActivity(@NonNull final Context context, @NonNull final ReportInfo reportInfo) { |
| 170 | + if(context == null) return; |
| 171 | + context.startActivity(newInstance(context, reportInfo)); |
| 172 | + } |
| 173 | + |
| 174 | + public static Intent newInstance(@NonNull final Context context, @NonNull final ReportInfo reportInfo) { |
| 175 | + if(context == null) return null; |
| 176 | + |
| 177 | + Intent intent = new Intent(context, ReportActivity.class); |
| 178 | + Bundle bundle = new Bundle(); |
| 179 | + bundle.putSerializable(ARG_REPORT_INFO, reportInfo); |
| 180 | + intent.putExtras(bundle); |
| 181 | + |
| 182 | + // Note that ReportActivity task has documentLaunchMode="intoExisting" set in AndroidManifest.xml |
| 183 | + // which has equivalent behaviour to the following. The following dynamic way doesn't seem to |
| 184 | + // work for notification pending intent, i.e separate task isn't created and activity is |
| 185 | + // launched in the same task as TermuxActivity. |
| 186 | + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NEW_DOCUMENT); |
| 187 | + return intent; |
| 188 | + } |
| 189 | + |
| 190 | +} |
0 commit comments