fedilab-Android-App/app/src/main/java/app/fedilab/android/fragments/DisplayScheduledTootsFragme...

240 lines
11 KiB
Java
Raw Normal View History

2019-05-18 11:10:30 +02:00
package app.fedilab.android.fragments;
2017-07-16 17:09:35 +02:00
/* Copyright 2017 Thomas Schneider
*
2019-05-18 11:10:30 +02:00
* This file is a part of Fedilab
2017-07-16 17:09:35 +02:00
*
* 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.
*
2019-05-18 11:10:30 +02:00
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
2017-07-16 17:09:35 +02:00
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
2019-05-18 11:10:30 +02:00
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
2017-07-16 17:09:35 +02:00
* see <http://www.gnu.org/licenses>. */
import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
2017-07-16 17:09:35 +02:00
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
2017-07-16 19:04:53 +02:00
import android.database.sqlite.SQLiteDatabase;
2017-07-16 17:09:35 +02:00
import android.os.AsyncTask;
import android.os.Build;
2017-07-16 17:09:35 +02:00
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
2019-06-11 19:38:26 +02:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import android.text.Html;
import android.text.Spanned;
2017-07-16 17:09:35 +02:00
import android.view.LayoutInflater;
import android.view.MotionEvent;
2017-07-16 17:09:35 +02:00
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
2019-01-19 19:41:55 +01:00
import android.widget.Toast;
2019-01-19 19:41:55 +01:00
import java.util.ArrayList;
2017-07-16 17:09:35 +02:00
import java.util.List;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.client.APIResponse;
import app.fedilab.android.client.Entities.StoredStatus;
import app.fedilab.android.drawers.ScheduledTootsListAdapter;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.sqlite.BoostScheduleDAO;
import app.fedilab.android.sqlite.Sqlite;
import app.fedilab.android.sqlite.StatusStoredDAO;
2019-01-19 19:41:55 +01:00
import es.dmoral.toasty.Toasty;
2019-05-18 11:10:30 +02:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveScheduledTootsAsyncTask;
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
import app.fedilab.android.interfaces.OnRetrieveScheduledTootsInterface;
2019-05-18 11:10:30 +02:00
import static app.fedilab.android.helper.Helper.changeDrawableColor;
2017-07-16 17:09:35 +02:00
/**
* Created by Thomas on 16/07/2017.
* Fragment to display scheduled toots
*/
2019-01-19 19:41:55 +01:00
public class DisplayScheduledTootsFragment extends Fragment implements OnRetrieveScheduledTootsInterface, OnRetrieveFeedsInterface {
2017-07-16 17:09:35 +02:00
private Context context;
private AsyncTask<Void, Void, Void> asyncTask;
private RelativeLayout mainLoader, textviewNoAction;
private ListView lv_scheduled_toots;
private TextView warning_battery_message;
private typeOfSchedule type;
2019-01-19 19:41:55 +01:00
private List<StoredStatus> storedStatuses;
private boolean firstCall;
private ScheduledTootsListAdapter scheduledTootsListAdapter;
public enum typeOfSchedule{
TOOT,
2019-01-19 19:41:55 +01:00
BOOST,
SERVER
}
2017-07-16 17:09:35 +02:00
@Override
2017-12-17 14:20:04 +01:00
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
2017-07-16 17:09:35 +02:00
View rootView = inflater.inflate(R.layout.fragment_scheduled_toots, container, false);
context = getContext();
Bundle bundle = this.getArguments();
assert bundle != null;
type = (typeOfSchedule) bundle.get("type");
if( type == null)
type = typeOfSchedule.TOOT;
2017-10-28 11:02:37 +02:00
lv_scheduled_toots = rootView.findViewById(R.id.lv_scheduled_toots);
2019-01-19 19:41:55 +01:00
firstCall = true;
2017-10-28 11:02:37 +02:00
mainLoader = rootView.findViewById(R.id.loader);
warning_battery_message = rootView.findViewById(R.id.warning_battery_message);
textviewNoAction = rootView.findViewById(R.id.no_action);
2017-07-16 17:09:35 +02:00
mainLoader.setVisibility(View.VISIBLE);
2019-01-19 19:41:55 +01:00
storedStatuses = new ArrayList<>();
2017-07-16 19:04:53 +02:00
//Removes all scheduled toots that have sent
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
if( type == typeOfSchedule.TOOT)
new StatusStoredDAO(context, db).removeAllSent();
else if( type == typeOfSchedule.BOOST)
new BoostScheduleDAO(context, db).removeAllSent();
2019-01-19 19:41:55 +01:00
else if( type == typeOfSchedule.SERVER)
asyncTask = new RetrieveFeedsAsyncTask(context, RetrieveFeedsAsyncTask.Type.SCHEDULED_TOOTS, null, DisplayScheduledTootsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
scheduledTootsListAdapter = new ScheduledTootsListAdapter(context, type, storedStatuses, textviewNoAction);
lv_scheduled_toots.setAdapter(scheduledTootsListAdapter);
2017-07-16 17:09:35 +02:00
return rootView;
}
2019-01-19 19:41:55 +01:00
@Override
public void onRetrieveFeeds(APIResponse apiResponse) {
if( apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 ){
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
return;
}
mainLoader.setVisibility(View.GONE);
if(apiResponse.getStoredStatuses() != null && apiResponse.getStoredStatuses().size() > 0 ){
storedStatuses.addAll(apiResponse.getStoredStatuses());
textviewNoAction.setVisibility(View.GONE);
lv_scheduled_toots.setVisibility(View.VISIBLE);
scheduledTootsListAdapter.notifyDataSetChanged();
}else if( firstCall){
textviewNoAction.setVisibility(View.VISIBLE);
lv_scheduled_toots.setVisibility(View.GONE);
}
firstCall = false;
}
2017-07-16 17:09:35 +02:00
2017-07-16 19:04:53 +02:00
@Override
public void onResume(){
super.onResume();
2019-01-20 15:20:07 +01:00
if( type != null && type != typeOfSchedule.SERVER) {
//Retrieves scheduled toots
asyncTask = new RetrieveScheduledTootsAsyncTask(context, type, DisplayScheduledTootsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
final PowerManager powerManager = (PowerManager) getActivity().getSystemService(Context.POWER_SERVICE);
final SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
//Battery saver is one and user never asked to stop showing the message
2019-05-18 11:10:30 +02:00
Helper.changeDrawableColor(context, R.drawable.ic_report, R.color.mastodonC4);
Helper.changeDrawableColor(context, R.drawable.ic_cancel, R.color.mastodonC4);
2019-01-20 15:20:07 +01:00
if (powerManager != null && powerManager.isPowerSaveMode() && sharedpreferences.getBoolean(Helper.SHOW_BATTERY_SAVER_MESSAGE, true)) {
warning_battery_message.setVisibility(View.VISIBLE);
} else {
warning_battery_message.setVisibility(View.GONE);
}
warning_battery_message.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP) {
if (event.getRawX() >= (warning_battery_message.getRight() - warning_battery_message.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putBoolean(Helper.SHOW_BATTERY_SAVER_MESSAGE, false);
editor.apply();
warning_battery_message.setVisibility(View.GONE);
return true;
}
}
2019-01-20 15:20:07 +01:00
return false;
}
2019-01-20 15:20:07 +01:00
});
warning_battery_message.setOnClickListener(new View.OnClickListener() {
@SuppressLint("BatteryLife")
@Override
public void onClick(View v) {
try {
2019-01-20 15:20:07 +01:00
Intent battSaverIntent = new Intent();
battSaverIntent.setComponent(new ComponentName("com.android.settings", "com.android.settings.Settings$BatterySaverSettingsActivity"));
startActivityForResult(battSaverIntent, 0);
} catch (ActivityNotFoundException e) {
try {
Intent batterySaverIntent;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP_MR1) {
batterySaverIntent = new Intent(Settings.ACTION_BATTERY_SAVER_SETTINGS);
startActivity(batterySaverIntent);
}
} catch (ActivityNotFoundException ignored) {
}
2019-01-20 15:20:07 +01:00
}
}
2019-01-20 15:20:07 +01:00
});
} else {
warning_battery_message.setVisibility(View.GONE);
}
}
2017-07-16 19:04:53 +02:00
}
2017-07-16 17:09:35 +02:00
@Override
public void onCreate(Bundle saveInstance)
{
super.onCreate(saveInstance);
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.context = context;
}
public void onDestroy() {
super.onDestroy();
if(asyncTask != null && asyncTask.getStatus() == AsyncTask.Status.RUNNING)
asyncTask.cancel(true);
}
@Override
public void onRetrieveScheduledToots(List<StoredStatus> storedStatuses) {
mainLoader.setVisibility(View.GONE);
if( storedStatuses != null && storedStatuses.size() > 0 ){
2019-01-19 19:41:55 +01:00
scheduledTootsListAdapter = new ScheduledTootsListAdapter(context, type, storedStatuses, textviewNoAction);
2017-07-16 17:09:35 +02:00
lv_scheduled_toots.setAdapter(scheduledTootsListAdapter);
2017-07-17 18:53:12 +02:00
textviewNoAction.setVisibility(View.GONE);
2017-07-16 17:09:35 +02:00
}else {
textviewNoAction.setVisibility(View.VISIBLE);
if( type == typeOfSchedule.BOOST) {
TextView no_action_text = textviewNoAction.findViewById(R.id.no_action_text);
TextView no_action_text_subtitle = textviewNoAction.findViewById(R.id.no_action_text_subtitle);
no_action_text.setText(context.getString(R.string.no_scheduled_boosts));
Spanned message;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
message = Html.fromHtml(context.getString(R.string.no_scheduled_boosts_indications), Html.FROM_HTML_MODE_LEGACY);
else
//noinspection deprecation
message = Html.fromHtml(context.getString(R.string.no_scheduled_boosts_indications));
no_action_text_subtitle.setText(message, TextView.BufferType.SPANNABLE);
}
2017-07-16 17:09:35 +02:00
}
}
}