|
1 | 1 | package com.atomjack.vcfp.activities;
|
2 | 2 |
|
| 3 | +import android.Manifest; |
3 | 4 | import android.app.Activity;
|
4 | 5 | import android.app.AlertDialog;
|
5 | 6 | import android.app.Dialog;
|
|
16 | 17 | import android.os.Environment;
|
17 | 18 | import android.os.Handler;
|
18 | 19 | import android.speech.tts.TextToSpeech;
|
| 20 | +import android.support.v4.app.ActivityCompat; |
| 21 | +import android.support.v4.content.ContextCompat; |
19 | 22 | import android.support.v7.media.MediaRouteSelector;
|
20 | 23 | import android.support.v7.media.MediaRouter;
|
21 | 24 | import android.view.LayoutInflater;
|
@@ -174,74 +177,102 @@ public void emailDeviceLogs(final String wearLog) {
|
174 | 177 | @Override
|
175 | 178 | protected Void doInBackground(Void... params) {
|
176 | 179 | try {
|
177 |
| - Logger.d("Emailing device logs"); |
178 |
| - Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
179 |
| - emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Voice Control for Plex Android Logs"); |
180 |
| - |
181 |
| - // Build the body of the email |
182 |
| - StringBuilder body = new StringBuilder(); |
183 |
| - body.append(String.format("Manufacturer: %s\n", Build.MANUFACTURER)); |
184 |
| - body.append(String.format("Device: %s\n", Build.DEVICE)); |
185 |
| - body.append(String.format("Model: %s\n", Build.MODEL)); |
186 |
| - body.append(String.format("Product: %s\n", Build.PRODUCT)); |
187 |
| - body.append(String.format("Version: %s\n\n", Build.VERSION.RELEASE)); |
188 |
| - |
189 |
| - body.append(String.format("Logged in: %s\n\n", VoiceControlForPlexApplication.getInstance().prefs.getString(Preferences.PLEX_USERNAME) != null ? "yes" : "no")); |
190 |
| - |
191 |
| - body.append("Description of the issue:\n\n"); |
192 |
| - |
193 |
| - emailIntent.setType("application/octet-stream"); |
194 |
| - |
195 |
| - emailIntent.putExtra(Intent.EXTRA_TEXT, body.toString()); |
196 |
| - |
197 |
| - File tempDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp"); |
198 |
| - if (!tempDirectory.exists()) |
199 |
| - tempDirectory.mkdirs(); |
200 |
| - |
201 |
| - File tempFile = new File(tempDirectory, "/vcfp-log.txt"); |
202 |
| - FileOutputStream fos = new FileOutputStream(tempFile); |
203 |
| - Writer out = new OutputStreamWriter(fos, "UTF-8"); |
204 |
| - |
205 |
| - Process process = Runtime.getRuntime().exec("logcat -d *:V"); |
206 |
| - BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
207 |
| - StringBuilder log = new StringBuilder(); |
208 |
| - String line; |
209 |
| - while ((line = bufferedReader.readLine()) != null) |
210 |
| - { |
211 |
| - log.append(line); |
212 |
| - log.append(System.getProperty("line.separator")); |
213 |
| - } |
| 180 | + boolean hasPermission = (ContextCompat.checkSelfPermission(MainActivity.this, |
| 181 | + Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED); |
| 182 | + if(!hasPermission) { |
| 183 | + ActivityCompat.requestPermissions(MainActivity.this, |
| 184 | + new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, |
| 185 | + REQUEST_WRITE_STORAGE); |
| 186 | + } else { |
| 187 | + Logger.d("Emailing device logs"); |
| 188 | + Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); |
| 189 | + emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Voice Control for Plex Android Logs"); |
| 190 | + |
| 191 | + // Build the body of the email |
| 192 | + StringBuilder body = new StringBuilder(); |
| 193 | + body.append(String.format("Manufacturer: %s\n", Build.MANUFACTURER)); |
| 194 | + body.append(String.format("Device: %s\n", Build.DEVICE)); |
| 195 | + body.append(String.format("Model: %s\n", Build.MODEL)); |
| 196 | + body.append(String.format("Product: %s\n", Build.PRODUCT)); |
| 197 | + body.append(String.format("Version: %s\n\n", Build.VERSION.RELEASE)); |
| 198 | + |
| 199 | + body.append(String.format("Logged in: %s\n\n", VoiceControlForPlexApplication.getInstance().prefs.getString(Preferences.PLEX_USERNAME) != null ? "yes" : "no")); |
| 200 | + |
| 201 | + body.append("Description of the issue:\n\n"); |
| 202 | + |
| 203 | + emailIntent.setType("application/octet-stream"); |
214 | 204 |
|
215 |
| - bufferedReader.close(); |
| 205 | + emailIntent.putExtra(Intent.EXTRA_TEXT, body.toString()); |
216 | 206 |
|
217 |
| - out.write(log.toString()); |
218 |
| - out.flush(); |
219 |
| - out.close(); |
| 207 | + File tempDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/tmp"); |
| 208 | + if (!tempDirectory.exists()) |
| 209 | + tempDirectory.mkdirs(); |
220 | 210 |
|
221 |
| - ArrayList<Uri> uris = new ArrayList<Uri>(); |
222 |
| - uris.add(Uri.parse("file://" + tempFile.getAbsolutePath())); |
| 211 | + File tempFile = new File(tempDirectory, "/vcfp-log.txt"); |
| 212 | + FileOutputStream fos = new FileOutputStream(tempFile); |
| 213 | + Writer out = new OutputStreamWriter(fos, "UTF-8"); |
223 | 214 |
|
224 |
| - if(!wearLog.equals("")) { |
225 |
| - tempFile = new File(tempDirectory, "/vcfp-wear-log.txt"); |
226 |
| - fos = new FileOutputStream(tempFile); |
227 |
| - out = new OutputStreamWriter(fos, "UTF-8"); |
228 |
| - out.write(wearLog); |
| 215 | + Process process = Runtime.getRuntime().exec("logcat -d *:V"); |
| 216 | + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream())); |
| 217 | + StringBuilder log = new StringBuilder(); |
| 218 | + String line; |
| 219 | + while ((line = bufferedReader.readLine()) != null) { |
| 220 | + log.append(line); |
| 221 | + log.append(System.getProperty("line.separator")); |
| 222 | + } |
| 223 | + |
| 224 | + bufferedReader.close(); |
| 225 | + |
| 226 | + out.write(log.toString()); |
229 | 227 | out.flush();
|
230 | 228 | out.close();
|
| 229 | + |
| 230 | + ArrayList<Uri> uris = new ArrayList<Uri>(); |
231 | 231 | uris.add(Uri.parse("file://" + tempFile.getAbsolutePath()));
|
| 232 | + |
| 233 | + if (!wearLog.equals("")) { |
| 234 | + tempFile = new File(tempDirectory, "/vcfp-wear-log.txt"); |
| 235 | + fos = new FileOutputStream(tempFile); |
| 236 | + out = new OutputStreamWriter(fos, "UTF-8"); |
| 237 | + out.write(wearLog); |
| 238 | + out.flush(); |
| 239 | + out.close(); |
| 240 | + uris.add(Uri.parse("file://" + tempFile.getAbsolutePath())); |
| 241 | + } |
| 242 | + emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
| 243 | + startActivity(emailIntent); |
232 | 244 | }
|
233 |
| - emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); |
234 |
| - startActivity(emailIntent); |
235 |
| - } catch (Exception ex) { |
236 |
| - Logger.e("Exception emailing device logs: %s", ex); |
237 |
| - feedback.e("Error emailing device logs: %s", ex.getMessage()); |
238 |
| - } |
239 |
| - return null; |
| 245 | + } catch (final Exception ex) { |
| 246 | + Logger.e("Exception emailing device logs: %s", ex); |
| 247 | + runOnUiThread(new Runnable() { |
| 248 | + @Override |
| 249 | + public void run() { |
| 250 | + feedback.e("Error emailing device logs: %s", ex.getMessage()); |
| 251 | + } |
| 252 | + }); |
| 253 | + } |
| 254 | + return null; |
240 | 255 | }
|
241 | 256 | }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
242 | 257 | }
|
243 | 258 |
|
244 |
| - public void showChangelog(MenuItem item) { |
| 259 | + @Override |
| 260 | + public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { |
| 261 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 262 | + switch (requestCode) { |
| 263 | + case REQUEST_WRITE_STORAGE: { |
| 264 | + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) |
| 265 | + { |
| 266 | + emailDeviceLogs(""); |
| 267 | + } else |
| 268 | + { |
| 269 | + feedback.e(R.string.email_device_logs_write_storage_denied); |
| 270 | + } |
| 271 | + } |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + public void showChangelog(MenuItem item) { |
245 | 276 | final WhatsNewDialog whatsNewDialog = new WhatsNewDialog(this);
|
246 | 277 | whatsNewDialog.forceShow();
|
247 | 278 | }
|
|
0 commit comments