From 72e686b2e53f8736cc83f4abf1e3e66fce0ad2b4 Mon Sep 17 00:00:00 2001 From: tom79 Date: Fri, 4 Aug 2017 10:12:02 +0200 Subject: [PATCH] Adds theKinrar's API for the search of instances --- .../mastodon/activities/AboutActivity.java | 9 +++ .../mastodon/activities/LoginActivity.java | 62 ++++++++++++++++++- .../etalab/mastodon/client/KinrarClient.java | 61 ++++++++++++++++++ .../gouv/etalab/mastodon/helper/Helper.java | 3 + .../res/layout-sw600dp/activity_about.xml | 24 +++++++ .../res/layout-sw600dp/activity_login.xml | 4 +- app/src/main/res/layout/activity_about.xml | 24 +++++++ app/src/main/res/layout/activity_login.xml | 2 +- app/src/main/res/values-fr/strings.xml | 2 + app/src/main/res/values/strings.xml | 3 + 10 files changed, 188 insertions(+), 6 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/client/KinrarClient.java diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/AboutActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/AboutActivity.java index 4de57b53a..89e2e6301 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/AboutActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/AboutActivity.java @@ -70,6 +70,7 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveSearcA about_developer = (Button) findViewById(R.id.about_developer); Button about_code = (Button) findViewById(R.id.about_code); Button about_license = (Button) findViewById(R.id.about_license); + Button about_thekinrar = (Button) findViewById(R.id.about_thekinrar); about_code.setOnClickListener(new View.OnClickListener() { @Override @@ -78,6 +79,14 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveSearcA 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())) { about_developer.setEnabled(false); new RetrieveDeveloperAccountsAsyncTask(getApplicationContext(),AboutActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java index b6567724d..e2ac9bc20 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/LoginActivity.java @@ -21,10 +21,13 @@ import android.graphics.Paint; import android.os.AsyncTask; import android.os.Bundle; 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.MenuItem; import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.AutoCompleteTextView; import android.widget.Button; import android.widget.EditText; 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.RequestParams; +import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; @@ -46,6 +50,7 @@ import java.security.UnrecoverableKeyException; import cz.msebera.android.httpclient.Header; 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.OauthClient; import fr.gouv.etalab.mastodon.helper.Helper; @@ -67,7 +72,7 @@ public class LoginActivity extends AppCompatActivity { private TextView login_two_step; private static boolean client_id_for_webview = false; private String instance; - private EditText login_instance; + private AutoCompleteTextView login_instance; @Override protected void onCreate(Bundle savedInstanceState) { @@ -87,7 +92,55 @@ public class LoginActivity extends AppCompatActivity { changeDrawableColor(getApplicationContext(), R.drawable.mastodon_icon, R.color.colorAccent); } 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 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); login_two_step = (TextView) findViewById(R.id.login_two_step); login_two_step.setVisibility(View.GONE); @@ -259,4 +312,7 @@ public class LoginActivity extends AppCompatActivity { return super.onOptionsItemSelected(item); } + + + } \ No newline at end of file diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/KinrarClient.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/KinrarClient.java new file mode 100644 index 000000000..11febbfe4 --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/KinrarClient.java @@ -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 . */ +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; + } + + +} \ No newline at end of file diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java index 2ec22507f..b45c0cf38 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/helper/Helper.java @@ -150,6 +150,9 @@ public class Helper { public static final int EXTERNAL_STORAGE_REQUEST_CODE = 84; 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 public static final String CLIENT_NAME = "client_name"; public static final String APP_PREFS = "app_prefs"; diff --git a/app/src/main/res/layout-sw600dp/activity_about.xml b/app/src/main/res/layout-sw600dp/activity_about.xml index 4cd7cfaf3..065ac392b 100644 --- a/app/src/main/res/layout-sw600dp/activity_about.xml +++ b/app/src/main/res/layout-sw600dp/activity_about.xml @@ -107,6 +107,30 @@ + + + + +