From c6edd8f1e997c33a48c74c2fc8a2d02d34e4be00 Mon Sep 17 00:00:00 2001 From: stom79 Date: Sat, 20 Oct 2018 12:22:35 +0200 Subject: Search on Peertube instances --- .../mastodon/activities/BaseMainActivity.java | 50 ++++++++++++++++-- .../mastodon/activities/PeertubeActivity.java | 2 - .../RetrievePeertubeSearchAsyncTask.java | 59 ++++++++++++++++++++++ .../java/fr/gouv/etalab/mastodon/client/API.java | 32 ++++++++++++ .../mastodon/fragments/DisplayStatusFragment.java | 37 ++++++++++---- 5 files changed, 162 insertions(+), 18 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrievePeertubeSearchAsyncTask.java (limited to 'app/src/main/java/fr/gouv') diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java index 9bb695831..e28538959 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/BaseMainActivity.java @@ -48,6 +48,7 @@ import android.support.v7.widget.SwitchCompat; import android.text.Editable; import android.text.InputFilter; import android.text.TextWatcher; +import android.util.Log; import android.util.Patterns; import android.view.Gravity; import android.view.LayoutInflater; @@ -195,6 +196,7 @@ public abstract class BaseMainActivity extends BaseActivity private String oldSearch; boolean isLoadingInstance = false; private ImageView delete_instance; + public static boolean displayPeertube = false; @Override protected void onCreate(Bundle savedInstanceState) { @@ -868,13 +870,50 @@ public abstract class BaseMainActivity extends BaseActivity InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); assert imm != null; imm.hideSoftInputFromWindow(toolbar_search.getWindowToken(), 0); - Intent intent = new Intent(BaseMainActivity.this, SearchResultActivity.class); + String peertube = null; + query= query.replaceAll("^#+", ""); - intent.putExtra("search", query); - startActivity(intent); + //It's not a peertube search + if(!displayPeertube){ + Intent intent = new Intent(BaseMainActivity.this, SearchResultActivity.class); + intent.putExtra("search", query); + startActivity(intent); + }else{ //Peertube search + if( main_app_container.getVisibility() == View.GONE){ + DisplayStatusFragment statusFragment; + Bundle bundle = new Bundle(); + statusFragment = new DisplayStatusFragment(); + bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE); + bundle.putString("remote_instance", toolbarTitle.getText().toString().trim()); + bundle.putString("search_peertube", query); + statusFragment.setArguments(bundle); + String fragmentTag = "REMOTE_INSTANCE"; + FragmentManager fragmentManager = getSupportFragmentManager(); + fragmentManager.beginTransaction() + .replace(R.id.main_app_container, statusFragment, fragmentTag).commit(); + main_app_container.setVisibility(View.VISIBLE); + toolbarTitle.setVisibility(View.VISIBLE); + delete_instance.setVisibility(View.VISIBLE); + viewPager.setVisibility(View.GONE); + tabLayout.setVisibility(View.GONE); + toolbarTitle.setText(instance); + }else{ + DisplayStatusFragment statusFragment; + Bundle bundle = new Bundle(); + statusFragment = new DisplayStatusFragment(); + bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE); + bundle.putString("remote_instance", toolbarTitle.getText().toString().trim()); + bundle.putString("search_peertube", query); + statusFragment.setArguments(bundle); + String fragmentTag = "REMOTE_INSTANCE"; + FragmentManager fragmentManager = getSupportFragmentManager(); + fragmentManager.beginTransaction() + .replace(R.id.main_app_container, statusFragment, fragmentTag).commit(); + } + toolbarTitle.setText(query + " - " + instance); + } toolbar_search.setQuery("", false); toolbar_search.setIconified(true); - if( main_app_container.getVisibility() == View.VISIBLE){ main_app_container.setVisibility(View.VISIBLE); viewPager.setVisibility(View.GONE); @@ -1629,6 +1668,7 @@ public abstract class BaseMainActivity extends BaseActivity if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { + displayPeertube = false; //Hide search bar on back pressed if( !toolbar_search.isIconified()){ toolbar_search.setIconified(true); @@ -1644,12 +1684,12 @@ public abstract class BaseMainActivity extends BaseActivity } else { Helper.switchLayout(BaseMainActivity.this); main_app_container.setVisibility(View.GONE); - viewPager.setVisibility(View.VISIBLE); tabLayout.setVisibility(View.VISIBLE); toolbarTitle.setVisibility(View.GONE); delete_instance.setVisibility(View.GONE); delete_all.hide(); + add_new.hide(); final NavigationView navigationView = findViewById(R.id.nav_view); unCheckAllMenuItems(navigationView); diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java index a4ec8cf1f..e8d9378ff 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/PeertubeActivity.java @@ -16,7 +16,6 @@ package fr.gouv.etalab.mastodon.activities; import android.Manifest; -import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -31,7 +30,6 @@ import android.support.v4.content.ContextCompat; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.text.Html; -import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrievePeertubeSearchAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrievePeertubeSearchAsyncTask.java new file mode 100644 index 000000000..f77e69e7d --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrievePeertubeSearchAsyncTask.java @@ -0,0 +1,59 @@ +/* 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 Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.asynctasks; + +import android.content.Context; +import android.os.AsyncTask; + +import java.lang.ref.WeakReference; + +import fr.gouv.etalab.mastodon.client.API; +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface; + + +/** + * Created by Thomas on 20/10/2018. + * Retrieves peertube search + */ + +public class RetrievePeertubeSearchAsyncTask extends AsyncTask { + + private String query, instance; + private APIResponse apiResponse; + private OnRetrieveFeedsInterface listener; + private WeakReference contextReference; + + public RetrievePeertubeSearchAsyncTask(Context context, String instance, String query, OnRetrieveFeedsInterface onRetrieveFeedsInterface){ + this.contextReference = new WeakReference<>(context); + this.query = query; + this.listener = onRetrieveFeedsInterface; + this.instance = instance; + } + + + @Override + protected Void doInBackground(Void... params) { + API api = new API(this.contextReference.get()); + apiResponse = api.searchPeertube(instance, query); + return null; + } + + @Override + protected void onPostExecute(Void result) { + listener.onRetrieveFeeds(apiResponse); + } + +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 7b9170885..222f21b39 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.client; import android.content.Context; import android.content.SharedPreferences; +import android.util.Log; import org.json.JSONArray; import org.json.JSONException; @@ -668,6 +669,37 @@ public class API { return apiResponse; } + /** + * Retrieves peertube search *synchronously* + * + * @param query String search + * @return APIResponse + */ + public APIResponse searchPeertube(String instance, String query) { + HashMap params = new HashMap<>(); + params.put("search", query); + params.put("count", "50"); + List peertubes = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get("https://"+instance+"/api/v1/search/videos", 60, params, null); + JSONArray jsonArray = new JSONObject(response).getJSONArray("data"); + peertubes = parsePeertube(instance, jsonArray); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } catch (NoSuchAlgorithmException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } catch (KeyManagementException e) { + e.printStackTrace(); + } catch (JSONException e) { + e.printStackTrace(); + } + apiResponse.setPeertubes(peertubes); + return apiResponse; + } /** * Retrieves Peertube videos from an instance *synchronously* * @return APIResponse diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java index ac79ab20f..1865aecf8 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayStatusFragment.java @@ -27,7 +27,6 @@ import android.support.v4.content.ContextCompat; import android.support.v4.widget.SwipeRefreshLayout; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; -import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -40,6 +39,7 @@ import fr.gouv.etalab.mastodon.R; import fr.gouv.etalab.mastodon.activities.BaseMainActivity; import fr.gouv.etalab.mastodon.activities.MainActivity; import fr.gouv.etalab.mastodon.asynctasks.RetrieveMissingFeedsAsyncTask; +import fr.gouv.etalab.mastodon.asynctasks.RetrievePeertubeSearchAsyncTask; import fr.gouv.etalab.mastodon.client.APIResponse; import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.client.Entities.Peertube; @@ -91,6 +91,9 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn private String remoteInstance; private List mutedAccount; private String instanceType; + private String search_peertube; + + public DisplayStatusFragment(){ } @@ -114,6 +117,7 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn showPinned = bundle.getBoolean("showPinned",false); showReply = bundle.getBoolean("showReply",false); remoteInstance = bundle.getString("remote_instance", ""); + search_peertube = bundle.getString("search_peertube", null); } SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); if( !remoteInstance.equals("")){ @@ -143,10 +147,12 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn mutedAccount = new TempMuteDAO(context, db).getAllTimeMuted(account); userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null); - if( instanceType == null || instanceType.equals("MASTODON")) { + if( search_peertube == null && (instanceType == null || instanceType.equals("MASTODON"))) { + BaseMainActivity.displayPeertube = false; statusListAdapter = new StatusListAdapter(context, type, targetedId, isOnWifi, behaviorWithAttachments, positionSpinnerTrans, this.statuses); lv_status.setAdapter(statusListAdapter); }else { + BaseMainActivity.displayPeertube = true; peertubeAdapater = new PeertubeAdapter(context, remoteInstance, this.peertubes); lv_status.setAdapter(peertubeAdapater); } @@ -170,9 +176,12 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, showReply, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); else if( type == RetrieveFeedsAsyncTask.Type.TAG) asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) - asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else + else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) { + if( search_peertube == null) + asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else + asyncTask = new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + }else asyncTask = new RetrieveFeedsAsyncTask(context, type, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); nextElementLoader.setVisibility(View.VISIBLE); @@ -246,9 +255,12 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, showReply,DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); else if (type == RetrieveFeedsAsyncTask.Type.TAG) asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) - asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else { + else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) { + if( search_peertube == null) + asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else + asyncTask = new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + }else { if( type == RetrieveFeedsAsyncTask.Type.HOME ){ String bookmark; if( context instanceof BaseMainActivity){ @@ -270,9 +282,12 @@ public class DisplayStatusFragment extends Fragment implements OnRetrieveFeedsIn asyncTask = new RetrieveFeedsAsyncTask(context, type, targetedId, max_id, showMediaOnly, showPinned, showReply,DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); else if (type == RetrieveFeedsAsyncTask.Type.TAG) asyncTask = new RetrieveFeedsAsyncTask(context, type, tag, targetedId, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) - asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - else { + else if( type == RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE ) { + if( search_peertube == null) + asyncTask = new RetrieveFeedsAsyncTask(context, type, remoteInstance, max_id, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + else + asyncTask = new RetrievePeertubeSearchAsyncTask(context, remoteInstance, search_peertube, DisplayStatusFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + }else { if( type == RetrieveFeedsAsyncTask.Type.HOME ){ String bookmark; if( context instanceof BaseMainActivity){ -- cgit v1.2.3