parent
0a32c58801
commit
4b8573a82f
@ -0,0 +1,158 @@ |
|||||||
|
/* Copyright 2017 Andrew Dawson |
||||||
|
* |
||||||
|
* This file is part of Tusky. |
||||||
|
* |
||||||
|
* Tusky is free software: you can redistribute it and/or modify it under the terms of the GNU |
||||||
|
* General Public License as published by the Free Software Foundation, either version 3 of the |
||||||
|
* License, or (at your option) any later version. |
||||||
|
* |
||||||
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
||||||
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
||||||
|
* Public License for more details. |
||||||
|
* |
||||||
|
* You should have received a copy of the GNU General Public License along with Tusky. If not, see |
||||||
|
* <http://www.gnu.org/licenses/>. */
|
||||||
|
|
||||||
|
package com.keylesspalace.tusky; |
||||||
|
|
||||||
|
import android.content.Context; |
||||||
|
import android.content.Intent; |
||||||
|
import android.content.SharedPreferences; |
||||||
|
import android.os.Bundle; |
||||||
|
import android.support.annotation.Nullable; |
||||||
|
import android.support.design.widget.Snackbar; |
||||||
|
import android.support.v7.app.ActionBar; |
||||||
|
import android.support.v7.widget.Toolbar; |
||||||
|
import android.view.View; |
||||||
|
import android.widget.Button; |
||||||
|
import android.widget.EditText; |
||||||
|
import android.widget.TextView; |
||||||
|
import android.widget.Toast; |
||||||
|
|
||||||
|
import com.android.volley.AuthFailureError; |
||||||
|
import com.android.volley.Request; |
||||||
|
import com.android.volley.Response; |
||||||
|
import com.android.volley.VolleyError; |
||||||
|
import com.android.volley.toolbox.JsonObjectRequest; |
||||||
|
|
||||||
|
import org.json.JSONArray; |
||||||
|
import org.json.JSONException; |
||||||
|
import org.json.JSONObject; |
||||||
|
|
||||||
|
import java.util.HashMap; |
||||||
|
import java.util.Map; |
||||||
|
|
||||||
|
public class ReportActivity extends BaseActivity { |
||||||
|
private static final String TAG = "ReportActivity"; // Volley request tag
|
||||||
|
|
||||||
|
private String domain; |
||||||
|
private String accessToken; |
||||||
|
private View anyView; // what Snackbar will use to find the root view
|
||||||
|
|
||||||
|
@Override |
||||||
|
protected void onCreate(@Nullable Bundle savedInstanceState) { |
||||||
|
super.onCreate(savedInstanceState); |
||||||
|
setContentView(R.layout.activity_report); |
||||||
|
|
||||||
|
Intent intent = getIntent(); |
||||||
|
final String accountId = intent.getStringExtra("account_id"); |
||||||
|
String accountUsername = intent.getStringExtra("account_username"); |
||||||
|
final String statusId = intent.getStringExtra("status_id"); |
||||||
|
String statusContent = intent.getStringExtra("status_content"); |
||||||
|
|
||||||
|
SharedPreferences preferences = getSharedPreferences( |
||||||
|
getString(R.string.preferences_file_key), Context.MODE_PRIVATE); |
||||||
|
domain = preferences.getString("domain", null); |
||||||
|
accessToken = preferences.getString("accessToken", null); |
||||||
|
|
||||||
|
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); |
||||||
|
setSupportActionBar(toolbar); |
||||||
|
ActionBar bar = getSupportActionBar(); |
||||||
|
if (bar != null) { |
||||||
|
String title = String.format(getString(R.string.report_username_format), |
||||||
|
accountUsername); |
||||||
|
bar.setTitle(title); |
||||||
|
} |
||||||
|
anyView = toolbar; |
||||||
|
|
||||||
|
TextView content = (TextView) findViewById(R.id.report_status_content); |
||||||
|
content.setText(HtmlUtils.fromHtml(statusContent)); |
||||||
|
|
||||||
|
final EditText comment = (EditText) findViewById(R.id.report_comment); |
||||||
|
Button send = (Button) findViewById(R.id.report_send); |
||||||
|
send.setOnClickListener(new View.OnClickListener() { |
||||||
|
@Override |
||||||
|
public void onClick(View v) { |
||||||
|
String[] statusIds = new String[] { statusId }; |
||||||
|
sendReport(accountId, statusIds, comment.getText().toString()); |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
/* JSONArray has a constructor to take primitive arrays but it's restricted to API level 19 and |
||||||
|
* above, so this is an alternative. */ |
||||||
|
private static JSONArray makeStringArrayCompat(String[] stringArray) throws JSONException { |
||||||
|
JSONArray result = new JSONArray(); |
||||||
|
for (int i = 0; i < stringArray.length; i++) { |
||||||
|
result.put(i, stringArray[i]); |
||||||
|
} |
||||||
|
return result; |
||||||
|
} |
||||||
|
|
||||||
|
private void sendReport(final String accountId, final String[] statusIds, |
||||||
|
final String comment) { |
||||||
|
JSONObject parameters = new JSONObject(); |
||||||
|
try { |
||||||
|
parameters.put("account_id", accountId); |
||||||
|
parameters.put("status_ids", makeStringArrayCompat(statusIds)); |
||||||
|
parameters.put("comment", comment); |
||||||
|
} catch (JSONException e) { |
||||||
|
Log.e(TAG, "Not all the report parameters have been properly set. " |
||||||
|
+ e.getMessage()); |
||||||
|
onSendFailure(accountId, statusIds, comment); |
||||||
|
return; |
||||||
|
} |
||||||
|
String endpoint = getString(R.string.endpoint_reports); |
||||||
|
String url = "https://" + domain + endpoint; |
||||||
|
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, parameters, |
||||||
|
new Response.Listener<JSONObject>() { |
||||||
|
@Override |
||||||
|
public void onResponse(JSONObject response) { |
||||||
|
onSendSuccess(); |
||||||
|
} |
||||||
|
}, |
||||||
|
new Response.ErrorListener() { |
||||||
|
@Override |
||||||
|
public void onErrorResponse(VolleyError error) { |
||||||
|
onSendFailure(accountId, statusIds, comment); |
||||||
|
} |
||||||
|
}) { |
||||||
|
@Override |
||||||
|
public Map<String, String> getHeaders() throws AuthFailureError { |
||||||
|
Map<String, String> headers = new HashMap<>(); |
||||||
|
headers.put("Authorization", "Bearer " + accessToken); |
||||||
|
return headers; |
||||||
|
} |
||||||
|
}; |
||||||
|
request.setTag(TAG); |
||||||
|
VolleySingleton.getInstance(this).addToRequestQueue(request); |
||||||
|
} |
||||||
|
|
||||||
|
private void onSendSuccess() { |
||||||
|
Toast.makeText(this, getString(R.string.confirmation_reported), Toast.LENGTH_SHORT) |
||||||
|
.show(); |
||||||
|
finish(); |
||||||
|
} |
||||||
|
|
||||||
|
private void onSendFailure(final String accountId, final String[] statusIds, |
||||||
|
final String comment) { |
||||||
|
Snackbar.make(anyView, R.string.error_report_unsent, Snackbar.LENGTH_LONG) |
||||||
|
.setAction(R.string.action_retry, new View.OnClickListener() { |
||||||
|
@Override |
||||||
|
public void onClick(View v) { |
||||||
|
sendReport(accountId, statusIds, comment); |
||||||
|
} |
||||||
|
}) |
||||||
|
.show(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,5 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<shape xmlns:android="http://schemas.android.com/apk/res/android"> |
||||||
|
<corners android:radius="3dp" /> |
||||||
|
<stroke android:color="#ffffffff" android:width="1dp" /> |
||||||
|
</shape> |
@ -0,0 +1,49 @@ |
|||||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||||
|
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||||
|
android:orientation="vertical" android:layout_width="match_parent" |
||||||
|
android:layout_height="match_parent"> |
||||||
|
|
||||||
|
<android.support.v7.widget.Toolbar |
||||||
|
android:id="@+id/toolbar" |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="?attr/actionBarSize" |
||||||
|
android:elevation="4dp" |
||||||
|
android:background="?attr/toolbar_background_color" /> |
||||||
|
|
||||||
|
<TextView |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:id="@+id/report_status_content" |
||||||
|
android:padding="8dp" |
||||||
|
android:background="?attr/report_status_background_color" /> |
||||||
|
|
||||||
|
<EditText |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="0dp" |
||||||
|
android:layout_weight="1" |
||||||
|
android:id="@+id/report_comment" |
||||||
|
android:inputType="textMultiLine" |
||||||
|
android:gravity="top|start" |
||||||
|
android:ems="10" |
||||||
|
android:layout_marginLeft="8dp" |
||||||
|
android:layout_marginRight="8dp" |
||||||
|
android:hint="@string/report_comment_hint" /> |
||||||
|
|
||||||
|
<Space |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="8dp" /> |
||||||
|
|
||||||
|
<RelativeLayout |
||||||
|
android:layout_width="match_parent" |
||||||
|
android:layout_height="wrap_content"> |
||||||
|
|
||||||
|
<Button |
||||||
|
android:layout_width="wrap_content" |
||||||
|
android:layout_height="wrap_content" |
||||||
|
android:layout_alignParentRight="true" |
||||||
|
android:id="@+id/report_send" |
||||||
|
android:text="@string/action_report" /> |
||||||
|
|
||||||
|
</RelativeLayout> |
||||||
|
|
||||||
|
</LinearLayout> |
Loading…
Reference in new issue