|
1 | 1 | package fr.free.nrw.commons.settings;
|
2 | 2 |
|
| 3 | +import android.Manifest; |
3 | 4 | import android.app.AlertDialog;
|
| 5 | +import android.content.ActivityNotFoundException; |
| 6 | +import android.content.Context; |
| 7 | +import android.content.Intent; |
4 | 8 | import android.content.SharedPreferences;
|
| 9 | +import android.content.pm.PackageManager; |
| 10 | +import android.net.Uri; |
| 11 | +import android.os.Build; |
5 | 12 | import android.os.Bundle;
|
6 | 13 | import android.preference.CheckBoxPreference;
|
7 | 14 | import android.preference.EditTextPreference;
|
8 | 15 | import android.preference.ListPreference;
|
| 16 | +import android.preference.Preference; |
9 | 17 | import android.preference.PreferenceFragment;
|
10 | 18 | import android.preference.PreferenceManager;
|
| 19 | +import android.support.annotation.NonNull; |
| 20 | +import android.support.v4.app.ActivityCompat; |
| 21 | +import android.support.v4.content.ContextCompat; |
| 22 | +import android.support.v4.content.FileProvider; |
| 23 | +import android.widget.Toast; |
11 | 24 |
|
| 25 | +import java.io.File; |
| 26 | + |
| 27 | +import fr.free.nrw.commons.BuildConfig; |
12 | 28 | import fr.free.nrw.commons.CommonsApplication;
|
13 | 29 | import fr.free.nrw.commons.R;
|
14 | 30 | import fr.free.nrw.commons.Utils;
|
| 31 | +import fr.free.nrw.commons.utils.FileUtils; |
15 | 32 |
|
16 | 33 | public class SettingsFragment extends PreferenceFragment {
|
| 34 | + |
| 35 | + private static final int REQUEST_CODE_WRITE_EXTERNAL_STORAGE = 100; |
| 36 | + |
17 | 37 | @Override
|
18 | 38 | public void onCreate(Bundle savedInstanceState) {
|
19 | 39 | super.onCreate(savedInstanceState);
|
@@ -66,6 +86,63 @@ public void onCreate(Bundle savedInstanceState) {
|
66 | 86 | return true;
|
67 | 87 | });
|
68 | 88 |
|
| 89 | + Preference sendLogsPreference = findPreference("sendLogFile"); |
| 90 | + sendLogsPreference.setOnPreferenceClickListener(preference -> { |
| 91 | + //first we need to check if we have the necessary permissions |
| 92 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { |
| 93 | + if (ContextCompat.checkSelfPermission( |
| 94 | + getActivity(), |
| 95 | + Manifest.permission.WRITE_EXTERNAL_STORAGE) |
| 96 | + == |
| 97 | + PackageManager.PERMISSION_GRANTED) { |
| 98 | + sendAppLogsViaEmail(); |
| 99 | + } else { |
| 100 | + //first get the necessary permission |
| 101 | + requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, |
| 102 | + REQUEST_CODE_WRITE_EXTERNAL_STORAGE); |
| 103 | + } |
| 104 | + } else { |
| 105 | + sendAppLogsViaEmail(); |
| 106 | + } |
| 107 | + |
| 108 | + return true; |
| 109 | + }); |
| 110 | + } |
| 111 | + |
| 112 | + @Override |
| 113 | + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { |
| 114 | + super.onRequestPermissionsResult(requestCode, permissions, grantResults); |
| 115 | + if (requestCode == REQUEST_CODE_WRITE_EXTERNAL_STORAGE) { |
| 116 | + if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { |
| 117 | + sendAppLogsViaEmail(); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private void sendAppLogsViaEmail() { |
| 123 | + String appLogs = Utils.getAppLogs(); |
| 124 | + File appLogsFile = FileUtils.createAndGetAppLogsFile(appLogs); |
| 125 | + |
| 126 | + Context applicationContext = getActivity().getApplicationContext(); |
| 127 | + Uri appLogsFilePath = FileProvider.getUriForFile( |
| 128 | + getActivity(), |
| 129 | + applicationContext.getPackageName() + ".provider", |
| 130 | + appLogsFile |
| 131 | + ); |
| 132 | + |
| 133 | + Intent feedbackIntent = new Intent(Intent.ACTION_SEND); |
| 134 | + feedbackIntent.setType("message/rfc822"); |
| 135 | + feedbackIntent.putExtra(Intent.EXTRA_EMAIL, |
| 136 | + new String[]{CommonsApplication.FEEDBACK_EMAIL}); |
| 137 | + feedbackIntent.putExtra(Intent.EXTRA_SUBJECT, |
| 138 | + String.format(CommonsApplication.FEEDBACK_EMAIL_SUBJECT, |
| 139 | + BuildConfig.VERSION_NAME)); |
| 140 | + feedbackIntent.putExtra(Intent.EXTRA_STREAM,appLogsFilePath); |
69 | 141 |
|
| 142 | + try { |
| 143 | + startActivity(feedbackIntent); |
| 144 | + } catch (ActivityNotFoundException e) { |
| 145 | + Toast.makeText(getActivity(), R.string.no_email_client, Toast.LENGTH_SHORT).show(); |
| 146 | + } |
70 | 147 | }
|
71 | 148 | }
|
0 commit comments