Adds theKinrar's API for the search of instances
This commit is contained in:
parent
5887a2fe20
commit
72e686b2e5
|
@ -70,6 +70,7 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveSearcA
|
||||||
about_developer = (Button) findViewById(R.id.about_developer);
|
about_developer = (Button) findViewById(R.id.about_developer);
|
||||||
Button about_code = (Button) findViewById(R.id.about_code);
|
Button about_code = (Button) findViewById(R.id.about_code);
|
||||||
Button about_license = (Button) findViewById(R.id.about_license);
|
Button about_license = (Button) findViewById(R.id.about_license);
|
||||||
|
Button about_thekinrar = (Button) findViewById(R.id.about_thekinrar);
|
||||||
|
|
||||||
about_code.setOnClickListener(new View.OnClickListener() {
|
about_code.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
|
@ -78,6 +79,14 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveSearcA
|
||||||
startActivity(browserIntent);
|
startActivity(browserIntent);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
about_thekinrar.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://instances.social/api/doc/"));
|
||||||
|
startActivity(browserIntent);
|
||||||
|
}
|
||||||
|
});
|
||||||
if(Helper.isLoggedIn(getApplicationContext())) {
|
if(Helper.isLoggedIn(getApplicationContext())) {
|
||||||
about_developer.setEnabled(false);
|
about_developer.setEnabled(false);
|
||||||
new RetrieveDeveloperAccountsAsyncTask(getApplicationContext(),AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
new RetrieveDeveloperAccountsAsyncTask(getApplicationContext(),AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
||||||
|
|
|
@ -21,10 +21,13 @@ import android.graphics.Paint;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
import android.util.Patterns;
|
import android.text.Editable;
|
||||||
|
import android.text.TextWatcher;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
|
import android.widget.ArrayAdapter;
|
||||||
|
import android.widget.AutoCompleteTextView;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
@ -34,6 +37,7 @@ import com.loopj.android.http.AsyncHttpClient;
|
||||||
import com.loopj.android.http.AsyncHttpResponseHandler;
|
import com.loopj.android.http.AsyncHttpResponseHandler;
|
||||||
import com.loopj.android.http.RequestParams;
|
import com.loopj.android.http.RequestParams;
|
||||||
|
|
||||||
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
|
|
||||||
|
@ -46,6 +50,7 @@ import java.security.UnrecoverableKeyException;
|
||||||
|
|
||||||
import cz.msebera.android.httpclient.Header;
|
import cz.msebera.android.httpclient.Header;
|
||||||
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
|
import fr.gouv.etalab.mastodon.asynctasks.UpdateAccountInfoAsyncTask;
|
||||||
|
import fr.gouv.etalab.mastodon.client.KinrarClient;
|
||||||
import fr.gouv.etalab.mastodon.client.MastalabSSLSocketFactory;
|
import fr.gouv.etalab.mastodon.client.MastalabSSLSocketFactory;
|
||||||
import fr.gouv.etalab.mastodon.client.OauthClient;
|
import fr.gouv.etalab.mastodon.client.OauthClient;
|
||||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||||
|
@ -67,7 +72,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
private TextView login_two_step;
|
private TextView login_two_step;
|
||||||
private static boolean client_id_for_webview = false;
|
private static boolean client_id_for_webview = false;
|
||||||
private String instance;
|
private String instance;
|
||||||
private EditText login_instance;
|
private AutoCompleteTextView login_instance;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -87,7 +92,55 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
changeDrawableColor(getApplicationContext(), R.drawable.mastodon_icon, R.color.colorAccent);
|
changeDrawableColor(getApplicationContext(), R.drawable.mastodon_icon, R.color.colorAccent);
|
||||||
}
|
}
|
||||||
final Button connectionButton = (Button) findViewById(R.id.login_button);
|
final Button connectionButton = (Button) findViewById(R.id.login_button);
|
||||||
login_instance = (EditText) findViewById(R.id.login_instance);
|
login_instance = (AutoCompleteTextView) findViewById(R.id.login_instance);
|
||||||
|
|
||||||
|
|
||||||
|
login_instance.addTextChangedListener(new TextWatcher() {
|
||||||
|
@Override
|
||||||
|
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
|
||||||
|
@Override
|
||||||
|
public void onTextChanged(CharSequence s, int start, int before, int count) {
|
||||||
|
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void afterTextChanged(Editable s) {
|
||||||
|
if( s.length() > 2 ){
|
||||||
|
String action = "/instances/search";
|
||||||
|
RequestParams parameters = new RequestParams();
|
||||||
|
parameters.add("q", s.toString().trim());
|
||||||
|
new KinrarClient().get(action, parameters, new AsyncHttpResponseHandler() {
|
||||||
|
@Override
|
||||||
|
public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
|
||||||
|
String response = new String(responseBody);
|
||||||
|
String[] instances;
|
||||||
|
try {
|
||||||
|
JSONObject jsonObject = new JSONObject(response);
|
||||||
|
JSONArray jsonArray = jsonObject.getJSONArray("instances");
|
||||||
|
if( jsonArray != null){
|
||||||
|
instances = new String[jsonArray.length()];
|
||||||
|
for(int i = 0 ; i < jsonArray.length() ; i++){
|
||||||
|
instances[i] = jsonArray.getJSONObject(i).get("name").toString();
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
instances = new String[]{};
|
||||||
|
}
|
||||||
|
login_instance.setAdapter(null);
|
||||||
|
ArrayAdapter<String> adapter =
|
||||||
|
new ArrayAdapter<>(LoginActivity.this, android.R.layout.simple_list_item_1, instances);
|
||||||
|
login_instance.setAdapter(adapter);
|
||||||
|
adapter.notifyDataSetChanged();
|
||||||
|
|
||||||
|
} catch (JSONException ignored) {}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
connectionButton.setEnabled(false);
|
connectionButton.setEnabled(false);
|
||||||
login_two_step = (TextView) findViewById(R.id.login_two_step);
|
login_two_step = (TextView) findViewById(R.id.login_two_step);
|
||||||
login_two_step.setVisibility(View.GONE);
|
login_two_step.setVisibility(View.GONE);
|
||||||
|
@ -259,4 +312,7 @@ public class LoginActivity extends AppCompatActivity {
|
||||||
return super.onOptionsItemSelected(item);
|
return super.onOptionsItemSelected(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
|
@ -0,0 +1,61 @@
|
||||||
|
/* Copyright 2017 Thomas Schneider
|
||||||
|
*
|
||||||
|
* This file is a part of Mastalab
|
||||||
|
*
|
||||||
|
* This program 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.
|
||||||
|
*
|
||||||
|
* Mastalab 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 Thomas Schneider; if not,
|
||||||
|
* see <http://www.gnu.org/licenses>. */
|
||||||
|
package fr.gouv.etalab.mastodon.client;
|
||||||
|
|
||||||
|
|
||||||
|
import com.loopj.android.http.AsyncHttpClient;
|
||||||
|
import com.loopj.android.http.AsyncHttpResponseHandler;
|
||||||
|
import com.loopj.android.http.RequestParams;
|
||||||
|
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.KeyStoreException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.security.UnrecoverableKeyException;
|
||||||
|
|
||||||
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||||
|
|
||||||
|
import static fr.gouv.etalab.mastodon.helper.Helper.USER_AGENT;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Thomas on 04/08/2017.
|
||||||
|
* Client for the Kinrar's API
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class KinrarClient {
|
||||||
|
|
||||||
|
private AsyncHttpClient client = new AsyncHttpClient();
|
||||||
|
|
||||||
|
|
||||||
|
public void get(String action, RequestParams params, AsyncHttpResponseHandler responseHandler) {
|
||||||
|
try {
|
||||||
|
client.setConnectTimeout(10000); //10s timeout
|
||||||
|
client.setUserAgent(USER_AGENT);
|
||||||
|
client.addHeader("Authorization", "Bearer " + Helper.THEKINRAR_SECRET_TOKEN);
|
||||||
|
MastalabSSLSocketFactory mastalabSSLSocketFactory = new MastalabSSLSocketFactory(MastalabSSLSocketFactory.getKeystore());
|
||||||
|
mastalabSSLSocketFactory.setHostnameVerifier(MastalabSSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
||||||
|
client.setSSLSocketFactory(mastalabSSLSocketFactory);
|
||||||
|
client.get(getAbsoluteUrl(action), params, responseHandler);
|
||||||
|
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException | UnrecoverableKeyException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private String getAbsoluteUrl(String action) {
|
||||||
|
return "https://instances.social/api/1.0/" + action;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
|
@ -150,6 +150,9 @@ public class Helper {
|
||||||
public static final int EXTERNAL_STORAGE_REQUEST_CODE = 84;
|
public static final int EXTERNAL_STORAGE_REQUEST_CODE = 84;
|
||||||
public static final int REQ_CODE_SPEECH_INPUT = 132;
|
public static final int REQ_CODE_SPEECH_INPUT = 132;
|
||||||
|
|
||||||
|
//Thekinrar's API: https://instances.social/api/doc/
|
||||||
|
public static final String THEKINRAR_SECRET_TOKEN = "jGj9gW3z9ptyIpB8CMGhAlTlslcemMV6AgoiImfw3vPP98birAJTHOWiu5ZWfCkLvcaLsFZw9e3Pb7TIwkbIyrj3z6S7r2oE6uy6EFHvls3YtapP8QKNZ980p9RfzTb4";
|
||||||
|
|
||||||
//Some definitions
|
//Some definitions
|
||||||
public static final String CLIENT_NAME = "client_name";
|
public static final String CLIENT_NAME = "client_name";
|
||||||
public static final String APP_PREFS = "app_prefs";
|
public static final String APP_PREFS = "app_prefs";
|
||||||
|
|
|
@ -107,6 +107,30 @@
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
<!-- About The Kinrar's API -->
|
||||||
|
<LinearLayout
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:padding="20dp"
|
||||||
|
android:gravity="center"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
<TextView
|
||||||
|
android:textSize="18sp"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:gravity="center"
|
||||||
|
android:clickable="true"
|
||||||
|
android:layout_width="200dp"
|
||||||
|
android:text="@string/about_thekinrar"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
<Button
|
||||||
|
android:id="@+id/about_thekinrar"
|
||||||
|
android:text="@string/about_thekinrar_action"
|
||||||
|
android:layout_width="150dp"
|
||||||
|
style="@style/Base.Widget.AppCompat.Button.Colored"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
<!-- About translation -->
|
<!-- About translation -->
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
|
|
|
@ -40,12 +40,12 @@
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
<EditText
|
<AutoCompleteTextView
|
||||||
android:id="@+id/login_instance"
|
android:id="@+id/login_instance"
|
||||||
android:layout_width="300dp"
|
android:layout_width="300dp"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:inputType="textWebEmailAddress"
|
android:inputType="textWebEmailAddress"
|
||||||
android:hint="@string/instance"
|
android:hint="@string/instance_example"
|
||||||
/>
|
/>
|
||||||
<EditText
|
<EditText
|
||||||
android:id="@+id/login_uid"
|
android:id="@+id/login_uid"
|
||||||
|
|
|
@ -110,6 +110,30 @@
|
||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
|
|
||||||
|
<!-- About The Kinrar's API -->
|
||||||
|
<LinearLayout
|
||||||
|
android:orientation="horizontal"
|
||||||
|
android:padding="10dp"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="wrap_content">
|
||||||
|
<TextView
|
||||||
|
android:textSize="16sp"
|
||||||
|
android:layout_gravity="center"
|
||||||
|
android:gravity="center"
|
||||||
|
android:clickable="true"
|
||||||
|
android:layout_weight="2"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
android:text="@string/about_thekinrar"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
<Button
|
||||||
|
android:id="@+id/about_thekinrar"
|
||||||
|
android:text="@string/about_thekinrar_action"
|
||||||
|
android:layout_weight="3"
|
||||||
|
android:layout_width="0dp"
|
||||||
|
style="@style/Base.Widget.AppCompat.Button.Colored"
|
||||||
|
android:layout_height="wrap_content" />
|
||||||
|
</LinearLayout>
|
||||||
|
|
||||||
<!-- About translation -->
|
<!-- About translation -->
|
||||||
<LinearLayout
|
<LinearLayout
|
||||||
android:orientation="horizontal"
|
android:orientation="horizontal"
|
||||||
|
|
|
@ -40,7 +40,7 @@
|
||||||
android:gravity="center"
|
android:gravity="center"
|
||||||
android:orientation="vertical"
|
android:orientation="vertical"
|
||||||
android:layout_height="wrap_content">
|
android:layout_height="wrap_content">
|
||||||
<EditText
|
<AutoCompleteTextView
|
||||||
android:id="@+id/login_instance"
|
android:id="@+id/login_instance"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
|
|
|
@ -175,6 +175,8 @@
|
||||||
<string name="about_code">Code source : </string>
|
<string name="about_code">Code source : </string>
|
||||||
<string name="about_code_action">bitbucket</string>
|
<string name="about_code_action">bitbucket</string>
|
||||||
<string name="about_yandex">Traduction des pouets :</string>
|
<string name="about_yandex">Traduction des pouets :</string>
|
||||||
|
<string name="about_thekinrar">Recherche d\'instances :</string>
|
||||||
|
<string name="about_thekinrar_action">API theKinrar</string>
|
||||||
<!-- Conversation -->
|
<!-- Conversation -->
|
||||||
<string name="conversation">Conversation</string>
|
<string name="conversation">Conversation</string>
|
||||||
|
|
||||||
|
|
|
@ -180,6 +180,9 @@
|
||||||
<string name="about_code_action">Bitbucket</string>
|
<string name="about_code_action">Bitbucket</string>
|
||||||
<string name="about_yandex">Translation of toots:</string>
|
<string name="about_yandex">Translation of toots:</string>
|
||||||
<string name="about_yandex_action" translatable="false">Powered by Yandex.Translate</string>
|
<string name="about_yandex_action" translatable="false">Powered by Yandex.Translate</string>
|
||||||
|
<string name="about_thekinrar">Search instances:</string>
|
||||||
|
<string name="about_thekinrar_action">theKinrar\'s API</string>
|
||||||
|
|
||||||
<!-- Conversation -->
|
<!-- Conversation -->
|
||||||
<string name="conversation">Conversation</string>
|
<string name="conversation">Conversation</string>
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue