From 5ee84010df2810ed2ee7b14fc0d094f5d5ec8112 Mon Sep 17 00:00:00 2001 From: stom79 Date: Mon, 7 Jan 2019 16:44:19 +0100 Subject: [PATCH] Finish front-end --- app/src/main/AndroidManifest.xml | 6 +- .../mastodon/activities/BaseMainActivity.java | 4 + .../activities/PeertubeUploadActivity.java | 136 +++++++++++++++++- .../RetrievePeertubeChannelsAsyncTask.java | 69 +++++++++ .../res/layout/activity_peertube_upload.xml | 4 +- 5 files changed, 215 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrievePeertubeChannelsAsyncTask.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3777d887f..13ec5cd19 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -234,7 +234,11 @@ android:theme="@style/Base.V7.Theme.AppCompat.Dialog" android:excludeFromRecents="true" /> - + . */ +import android.Manifest; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; +import android.content.pm.PackageManager; +import android.database.Cursor; +import android.net.Uri; +import android.os.AsyncTask; +import android.os.Build; import android.os.Bundle; +import android.provider.OpenableColumns; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.ContextCompat; import android.support.v7.app.ActionBar; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.ArrayAdapter; +import android.widget.Button; import android.widget.ImageView; +import android.widget.Spinner; import android.widget.TextView; +import android.widget.Toast; +import java.io.File; +import java.util.HashMap; +import java.util.List; + +import es.dmoral.toasty.Toasty; import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.asynctasks.RetrievePeertubeChannelsAsyncTask; +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.client.Entities.Account; import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeInterface; import static fr.gouv.etalab.mastodon.helper.Helper.THEME_LIGHT; -public class PeertubeUploadActivity extends BaseActivity{ +public class PeertubeUploadActivity extends BaseActivity implements OnRetrievePeertubeInterface { - private final int PICK_IVDEO = 567786; + private final int PICK_IVDEO = 52378; + private Button set_upload_file, set_upload_submit; + private Spinner set_upload_privacy, set_upload_channel; + private TextView set_upload_file_name; + private HashMap channels; + private final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 724; @Override protected void onCreate(Bundle savedInstanceState) { @@ -83,7 +110,14 @@ public class PeertubeUploadActivity extends BaseActivity{ } setContentView(R.layout.activity_peertube_upload); + set_upload_file = findViewById(R.id.set_upload_file); + set_upload_file_name = findViewById(R.id.set_upload_file_name); + set_upload_channel = findViewById(R.id.set_upload_channel); + set_upload_privacy = findViewById(R.id.set_upload_privacy); + set_upload_submit = findViewById(R.id.set_upload_submit); + new RetrievePeertubeChannelsAsyncTask(PeertubeUploadActivity.this, PeertubeUploadActivity.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + channels = new HashMap<>(); } @@ -92,7 +126,105 @@ public class PeertubeUploadActivity extends BaseActivity{ public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PICK_IVDEO && resultCode == Activity.RESULT_OK) { + if (data == null || data.getData() == null) { + Toasty.error(getApplicationContext(),getString(R.string.toot_select_image_error),Toast.LENGTH_LONG).show(); + return; + } + set_upload_submit.setEnabled(true); + + Uri uri = data.getData(); + String uriString = uri.toString(); + File myFile = new File(uriString); + String filename = null; + if (uriString.startsWith("content://")) { + Cursor cursor = null; + try { + cursor = getContentResolver().query(uri, null, null, null, null); + if (cursor != null && cursor.moveToFirst()) { + filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); + } + } finally { + assert cursor != null; + cursor.close(); + } + } else if (uriString.startsWith("file://")) { + filename = myFile.getName(); + } + if( filename != null) { + set_upload_file_name.setVisibility(View.VISIBLE); + set_upload_file_name.setText(filename); + } } } + + @Override + public void onRetrievePeertube(APIResponse apiResponse) { + if( apiResponse.getError() != null || apiResponse.getAccounts() == null || apiResponse.getAccounts().size() == 0){ + if ( apiResponse.getError().getError() != null) + Toasty.error(PeertubeUploadActivity.this, apiResponse.getError().getError(), Toast.LENGTH_LONG).show(); + else + Toasty.error(PeertubeUploadActivity.this, getString(R.string.toast_error), Toast.LENGTH_LONG).show(); + return; + } + + //Populate channels + List accounts = apiResponse.getAccounts(); + String[] channelName = new String[accounts.size()]; + int i = 0; + for(Account account: accounts){ + channels.put(account.getUsername(),account.getId()); + channelName[i] = account.getUsername(); + i++; + } + ArrayAdapter adapterChannel = new ArrayAdapter<>(PeertubeUploadActivity.this, + android.R.layout.simple_spinner_dropdown_item, channelName); + set_upload_channel.setAdapter(adapterChannel); + + //Populate privacy + String[] privacyName = new String[3]; + privacyName[0] = getString(R.string.v_public); + privacyName[1] = getString(R.string.v_unlisted); + privacyName[2] = getString(R.string.v_private); + ArrayAdapter adapterPrivacy = new ArrayAdapter<>(PeertubeUploadActivity.this, + android.R.layout.simple_spinner_dropdown_item, privacyName); + set_upload_privacy.setAdapter(adapterPrivacy); + + set_upload_file.setEnabled(true); + + set_upload_file.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + if (ContextCompat.checkSelfPermission(PeertubeUploadActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != + PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(PeertubeUploadActivity.this, + new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, + MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); + return; + } + } + Intent intent = new Intent(Intent.ACTION_GET_CONTENT); + intent.addCategory(Intent.CATEGORY_OPENABLE); + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { + intent.setType("*/*"); + String[] mimetypes = {"video/*"}; + intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes); + startActivityForResult(intent, PICK_IVDEO); + }else { + intent.setType("video/*"); + Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); + Intent chooserIntent = Intent.createChooser(intent, getString(R.string.toot_select_image)); + chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent}); + startActivityForResult(chooserIntent, PICK_IVDEO); + } + + } + }); + } + + @Override + public void onRetrievePeertubeComments(APIResponse apiResponse) { + + } } diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrievePeertubeChannelsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrievePeertubeChannelsAsyncTask.java new file mode 100644 index 000000000..ddfe14d3f --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrievePeertubeChannelsAsyncTask.java @@ -0,0 +1,69 @@ +/* Copyright 2019 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.content.SharedPreferences; +import android.database.sqlite.SQLiteDatabase; +import android.os.AsyncTask; + +import java.lang.ref.WeakReference; + +import fr.gouv.etalab.mastodon.client.APIResponse; +import fr.gouv.etalab.mastodon.client.Entities.Account; +import fr.gouv.etalab.mastodon.client.PeertubeAPI; +import fr.gouv.etalab.mastodon.helper.Helper; +import fr.gouv.etalab.mastodon.interfaces.OnRetrievePeertubeInterface; +import fr.gouv.etalab.mastodon.sqlite.AccountDAO; +import fr.gouv.etalab.mastodon.sqlite.Sqlite; + + +/** + * Created by Thomas on 07/01/2019. + * Retrieves peertube Channels + */ + +public class RetrievePeertubeChannelsAsyncTask extends AsyncTask { + + + + private APIResponse apiResponse; + private OnRetrievePeertubeInterface listener; + private WeakReference contextReference; + + + + + public RetrievePeertubeChannelsAsyncTask(Context context, OnRetrievePeertubeInterface onRetrievePeertubeInterface){ + this.contextReference = new WeakReference<>(context); + this.listener = onRetrievePeertubeInterface; + } + + @Override + protected Void doInBackground(Void... params) { + PeertubeAPI peertubeAPI = new PeertubeAPI(this.contextReference.get()); + SQLiteDatabase db = Sqlite.getInstance(contextReference.get(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open(); + SharedPreferences sharedpreferences = contextReference.get().getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE); + String token = sharedpreferences.getString(Helper.PREF_KEY_OAUTH_TOKEN, null); + Account account = new AccountDAO(contextReference.get(), db).getAccountByToken(token); + apiResponse = peertubeAPI.getPeertubeChannel(account.getUsername()); + return null; + } + + @Override + protected void onPostExecute(Void result) { + listener.onRetrievePeertube(apiResponse); + } +} diff --git a/app/src/main/res/layout/activity_peertube_upload.xml b/app/src/main/res/layout/activity_peertube_upload.xml index 14ba39705..bab0f3c79 100644 --- a/app/src/main/res/layout/activity_peertube_upload.xml +++ b/app/src/main/res/layout/activity_peertube_upload.xml @@ -32,9 +32,10 @@