Created
November 23, 2017 09:18
-
-
Save mohamed-khaled-hsn/7bc3aaff18811d8e18f98b7e2ca93c69 to your computer and use it in GitHub Desktop.
Feedback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.Manifest; | |
import android.app.Activity; | |
import android.content.Intent; | |
import android.content.pm.PackageManager; | |
import android.graphics.Point; | |
import android.net.Uri; | |
import android.os.Build; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v4.app.Fragment; | |
import android.support.v4.content.FileProvider; | |
import android.util.Log; | |
import android.view.Display; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.view.WindowManager; | |
import android.widget.CheckBox; | |
import android.widget.EditText; | |
import android.widget.ImageView; | |
import com.shippertag.BuildConfig; | |
import com.shippertag.R; | |
import com.shippertag.utils.Helper; | |
import com.shippertag.utils.L; | |
import com.shippertag.utils.QuickMessage; | |
import com.shippertag.utils.Translator; | |
import com.squareup.picasso.Picasso; | |
import java.io.File; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
public class FeedbackFragment extends Fragment { | |
private static final int READ_PERMISSION_REQUEST_CODE = 10; | |
private static final int EMAIL_REQUEST_CODE = 9; | |
private static final int PICK_PHOTO_REQUEST_CODE = 100; | |
private boolean mailClientOpened = false; | |
private String feedbackContent = ""; | |
private File logFile; | |
ImageView imageViewLeft, imageViewCenter, imageViewRight; | |
CheckBox systemLogsCheckBox; | |
EditText feedbackEditText; | |
private int clickedImageView; | |
private ArrayList<String> imagesToAttach; | |
View view; | |
@Override | |
public View onCreateView(LayoutInflater inflater, ViewGroup container, | |
Bundle savedInstanceState) { | |
// Inflate the layout for this fragment | |
view = inflater.inflate(R.layout.activity_feedback, container, false); | |
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN); | |
feedbackEditText = view.findViewById(R.id.feedback); | |
systemLogsCheckBox = view.findViewById(R.id.logs_checkbox); | |
imageViewLeft = view.findViewById(R.id.image_left); | |
imageViewCenter = view.findViewById(R.id.image_center); | |
imageViewRight = view.findViewById(R.id.image_right); | |
imagesToAttach = new ArrayList<>(3); | |
imagesToAttach.add(null); | |
imagesToAttach.add(null); | |
imagesToAttach.add(null); | |
View[] imageViews = {imageViewLeft, imageViewCenter, imageViewRight}; | |
for (final View imageView : imageViews) { | |
imageView.setOnClickListener(v -> { | |
clickedImageView = v.getId(); | |
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { | |
if (Helper.isPermissionGranted(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) | |
startGalleryActivity(); | |
else | |
Helper.requestPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE, READ_PERMISSION_REQUEST_CODE); | |
} else { | |
startGalleryActivity(); | |
} | |
}); | |
} | |
view.findViewById(R.id.send_button).setOnClickListener(v -> { | |
if (systemLogsCheckBox.isChecked()) | |
getLogs(); | |
feedbackContent = feedbackEditText.getText().toString().trim(); | |
startEmailActivity(); | |
}); | |
// L.v(getInfoAboutDevice()); | |
// getLogs(this); | |
return view; | |
} | |
private void startGalleryActivity() { | |
Intent pickPhoto = new Intent(Intent.ACTION_PICK, | |
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); | |
startActivityForResult(pickPhoto, PICK_PHOTO_REQUEST_CODE); | |
} | |
public String getInfoAboutDevice() { | |
StringBuilder s = new StringBuilder(); | |
s.append("\n\n_____________________________________" + | |
"\nPlease keep the following information\n"); | |
s.append("\n App Name: ").append(getActivity().getApplicationInfo().loadLabel(getActivity().getPackageManager()).toString()); | |
s.append("\n App Version Name: ").append(BuildConfig.VERSION_NAME); | |
s.append("\n App Version Code: ").append(BuildConfig.VERSION_CODE); | |
s.append("\n"); | |
s.append("\n OS Version: ").append(System.getProperty("os.version")).append(" (").append(android.os.Build.VERSION.INCREMENTAL).append(")"); | |
s.append("\n OS API Level: ").append(Build.VERSION.SDK_INT); | |
s.append("\n Device: ").append(android.os.Build.DEVICE); | |
s.append("\n Model (and Product): ").append(android.os.Build.MODEL).append(" (").append(android.os.Build.PRODUCT).append(")"); | |
s.append("\n Manufacturer: ").append(android.os.Build.MANUFACTURER); | |
Display display = getActivity().getWindowManager().getDefaultDisplay(); | |
Point size = new Point(); | |
display.getSize(size); | |
s.append("\n screenHeight: ").append(size.y); | |
s.append("\n screenWidth: ").append(size.x); | |
return s.toString(); | |
} | |
public void getLogs() { | |
// save logcat in file | |
// logFile = new File(getExternalCacheDir(), "logcat.txt"); | |
try { | |
logFile = File.createTempFile("shipperTag_logs", ".txt", getActivity().getExternalCacheDir()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
QuickMessage.shortToast(getContext(), Translator.getLanguageValueFromKey("error_creating_file", "Error occurred while creating the File")); | |
} | |
// Log.i("SendLogcatMail: ", "logcat file is located at: " + Uri.fromFile(logFile)); | |
// Log.i("DeviceInfo: ", "Device info:" + getInfoAboutDevice()); | |
try { | |
Runtime.getRuntime().exec( | |
"logcat -f " + logFile.getAbsolutePath() + " -t 500"); //get the last 500 line and dump it to logFile in cache directory | |
} catch (IOException e) { | |
e.printStackTrace(); | |
Log.e(getActivity().getLocalClassName(), "Error excuting logcat -f command ", e); | |
} | |
} | |
private void startEmailActivity() { | |
Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); | |
// emailIntent.setData(Uri.parse("mailto:")); // only email apps should handle this | |
emailIntent.setType("text/xml"); | |
// emailIntent.setType("message/rfc822"); | |
// emailIntent.setType("text/plain"); | |
String to[] = {"[email protected]"}; | |
emailIntent.putExtra(Intent.EXTRA_EMAIL, to); | |
// body | |
// ArrayList<String> extra_text = new ArrayList<>(); | |
// extra_text.add(feedbackContent + getInfoAboutDevice()); | |
// emailIntent.putStringArrayListExtra(Intent.EXTRA_TEXT, extra_text); | |
emailIntent.putExtra(Intent.EXTRA_TEXT, feedbackContent + getInfoAboutDevice()); | |
//attachments | |
ArrayList<Uri> uris = new ArrayList<>(); | |
//convert from paths to Android friendly Parcelable Uri's | |
for (String file : imagesToAttach) { | |
if (file != null) { | |
File fileIn = new File(file); | |
//Uri photoURI = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".my.package.name.provider", createImageFile()); | |
Uri u = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".generic.fileprovider", fileIn); | |
uris.add(u); | |
} | |
} | |
if (systemLogsCheckBox.isChecked() && logFile != null) | |
uris.add(FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID + ".generic.fileprovider", logFile)); | |
if (!uris.isEmpty()) | |
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); | |
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); | |
emailIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); | |
// the mail subject | |
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "ShipperTag Android Feedback"); | |
// startActivity(Intent.createChooser(emailIntent, "Choose app")); | |
if (emailIntent.resolveActivity(getActivity().getPackageManager()) != null) { | |
startActivityForResult(emailIntent, EMAIL_REQUEST_CODE); | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) { | |
switch (requestCode) { | |
case READ_PERMISSION_REQUEST_CODE: | |
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
startGalleryActivity(); | |
} | |
} | |
} | |
@Override | |
public void onResume() { | |
super.onResume(); | |
mailClientOpened = false; | |
} | |
@Override | |
public void onStop() { | |
super.onStop(); | |
mailClientOpened = true; | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
switch (requestCode) { | |
case EMAIL_REQUEST_CODE: | |
if (mailClientOpened) { | |
QuickMessage.shortToast(getContext(), Translator.getLanguageValueFromKey("thanks_for_feedback", "Thanks for feedback")); | |
getActivity().getSupportFragmentManager().popBackStack(); | |
} else { | |
// QuickMessage.shortToast(this, "Something went wrong, unable to send feedback"); | |
//TODO | |
} | |
break; | |
case PICK_PHOTO_REQUEST_CODE: | |
if (resultCode == Activity.RESULT_OK) { | |
if (data != null && data.getData() != null) { | |
// this case will occur in case of picking image from the Gallery, | |
// but not when taking picture with a camera | |
Uri imagePicked = data.getData(); | |
String imagePath = Helper.getRealPathFromUri(getActivity(), imagePicked); | |
L.v("image path: " + imagePath); | |
switch (clickedImageView) { | |
case R.id.image_left: | |
imagesToAttach.set(0, imagePath); | |
break; | |
case R.id.image_center: | |
imagesToAttach.set(1, imagePath); | |
break; | |
case R.id.image_right: | |
imagesToAttach.set(2, imagePath); | |
break; | |
default: | |
break; | |
} | |
try { | |
Picasso | |
.with(getContext()) | |
.load(imagePicked) | |
.fit() | |
.centerInside() | |
.into((ImageView) view.findViewById(clickedImageView)); | |
// do whatever you want with the Bitmap .... | |
} catch (Exception e) { | |
QuickMessage.shortToast(getContext(), Translator.getLanguageValueFromKey("error_display_image’", "Error displaying image")); | |
e.printStackTrace(); | |
} | |
} | |
} | |
break; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment