keys = fields.keys();
post.addFormDataPart("Content-Type", "text/plain");
while (keys.hasNext()) {
String key = keys.next();
post.addFormDataPart(key, fields.getString(key));
}
post.addFormDataPart("file", "file", RequestBody.create(MediaType.parse("text/plain"), paste));
Response postResponse = client.newCall(new Request.Builder().url(url).post(post.build()).build()).execute();
if (!postResponse.isSuccessful()) {
throw new IOException("Bad response: " + postResponse);
}
return API_ENDPOINT + "/" + item;
} catch (IOException | JSONException e) {
Log.w("ImageActivity", e);
}
return null;
}
@Override
protected void onPostExecute(final String response) {
super.onPostExecute(response);
if (response != null)
handleShowSuccessDialog(response);
else {
Log.w(TAG, "Response was null from Gist API.");
Toast.makeText(getActivity(), R.string.log_submit_activity__network_failure, Toast.LENGTH_LONG).show();
}
}
}
private static long asMegs(long bytes) {
return bytes / 1048576L;
}
public static String getMemoryUsage(Context context) {
Runtime info = Runtime.getRuntime();
info.totalMemory();
return String.format(Locale.ENGLISH, "%dM (%.2f%% free, %dM max)",
asMegs(info.totalMemory()),
(float)info.freeMemory() / info.totalMemory() * 100f,
asMegs(info.maxMemory()));
}
@TargetApi(VERSION_CODES.KITKAT)
public static String getMemoryClass(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
String lowMem = "";
if (VERSION.SDK_INT >= VERSION_CODES.KITKAT && activityManager.isLowRamDevice()) {
lowMem = ", low-mem device";
}
return activityManager.getMemoryClass() + lowMem;
}
private static String buildDescription(Context context) {
final PackageManager pm = context.getPackageManager();
final StringBuilder builder = new StringBuilder();
builder.append("Device : ")
.append(Build.MANUFACTURER).append(" ")
.append(Build.MODEL).append(" (")
.append(Build.PRODUCT).append(")\n");
builder.append("Android : ").append(VERSION.RELEASE).append(" (")
.append(VERSION.INCREMENTAL).append(", ")
.append(Build.DISPLAY).append(")\n");
builder.append("Memory : ").append(getMemoryUsage(context)).append("\n");
builder.append("Memclass: ").append(getMemoryClass(context)).append("\n");
builder.append("OS Host : ").append(Build.HOST).append("\n");
builder.append("App : ");
try {
builder.append(pm.getApplicationLabel(pm.getApplicationInfo(context.getPackageName(), 0)))
.append(" ")
.append(pm.getPackageInfo(context.getPackageName(), 0).versionName)
.append("\n");
} catch (PackageManager.NameNotFoundException nnfe) {
builder.append("Unknown\n");
}
return builder.toString();
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
*
* See the Android Training lesson Communicating with Other Fragments for more information.
*/
public interface OnLogSubmittedListener {
public void onSuccess();
public void onFailure();
public void onCancel();
}
private static final class LogPreviewAdapter extends RecyclerView.Adapter {
private String[] lines = new String[0];
@Override
public LogPreviewViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new LogPreviewViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_log_preview, parent, false));
}
@Override
public void onBindViewHolder(LogPreviewViewHolder holder, int position) {
holder.bind(lines, position);
}
@Override
public void onViewRecycled(LogPreviewViewHolder holder) {
holder.unbind();
}
@Override
public int getItemCount() {
return lines.length;
}
void setText(@NonNull String text) {
lines = text.split("\n");
notifyDataSetChanged();
}
String getText() {
return Util.join(lines, "\n");
}
}
private static final class LogPreviewViewHolder extends RecyclerView.ViewHolder {
private EditText text;
private String[] lines;
private int index;
LogPreviewViewHolder(View itemView) {
super(itemView);
text = (EditText) itemView;
}
void bind(String[] lines, int index) {
this.lines = lines;
this.index = index;
text.setText(lines[index]);
text.addTextChangedListener(textWatcher);
}
void unbind() {
text.removeTextChangedListener(textWatcher);
}
private final SimpleTextWatcher textWatcher = new SimpleTextWatcher() {
@Override
public void onTextChanged(String text) {
if (lines != null) {
lines[index] = text;
}
}
};
}
}