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

235 lines
10 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;
import android.os.Build;
2017-07-16 17:09:35 +02:00
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
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-11-15 16:32:25 +01:00
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
2020-07-04 19:18:44 +02:00
import org.jetbrains.annotations.NotNull;
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-11-15 16:32:25 +01:00
import app.fedilab.android.R;
import app.fedilab.android.asynctasks.RetrieveFeedsAsyncTask;
import app.fedilab.android.asynctasks.RetrieveScheduledTootsAsyncTask;
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;
2019-11-15 16:32:25 +01:00
import app.fedilab.android.interfaces.OnRetrieveFeedsInterface;
import app.fedilab.android.interfaces.OnRetrieveScheduledTootsInterface;
2019-05-18 11:10:30 +02:00
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;
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 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;
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");
2019-09-06 17:55:14 +02:00
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
2020-04-09 18:57:12 +02:00
SQLiteDatabase db = Sqlite.getInstance(context.getApplicationContext(), Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
2019-09-06 17:55:14 +02:00
if (type == typeOfSchedule.TOOT)
new StatusStoredDAO(context, db).removeAllSent();
2019-09-06 17:55:14 +02:00
else if (type == typeOfSchedule.BOOST)
new BoostScheduleDAO(context, db).removeAllSent();
2019-09-06 17:55:14 +02:00
else if (type == typeOfSchedule.SERVER)
2021-01-19 17:43:51 +01:00
new RetrieveFeedsAsyncTask(context, RetrieveFeedsAsyncTask.Type.SCHEDULED_TOOTS, null, DisplayScheduledTootsFragment.this);
2019-01-19 19:41:55 +01:00
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) {
2020-01-18 18:23:09 +01:00
if (apiResponse.getError() != null && apiResponse.getError().getStatusCode() != 404 && apiResponse.getError().getStatusCode() != 501) {
2019-11-15 16:32:25 +01:00
if (apiResponse.getError().getError().length() < 100) {
2019-09-30 18:14:46 +02:00
Toasty.error(context, apiResponse.getError().getError(), Toast.LENGTH_LONG).show();
2019-11-15 16:32:25 +01:00
} else {
Toasty.error(context, getString(R.string.long_api_error, "\ud83d\ude05"), Toast.LENGTH_LONG).show();
2019-09-30 18:14:46 +02:00
}
2019-01-19 19:41:55 +01:00
return;
}
mainLoader.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
if (apiResponse.getStoredStatuses() != null && apiResponse.getStoredStatuses().size() > 0) {
2019-01-19 19:41:55 +01:00
storedStatuses.addAll(apiResponse.getStoredStatuses());
textviewNoAction.setVisibility(View.GONE);
lv_scheduled_toots.setVisibility(View.VISIBLE);
scheduledTootsListAdapter.notifyDataSetChanged();
2019-09-06 17:55:14 +02:00
} else if (firstCall) {
2019-01-19 19:41:55 +01:00
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
2019-09-06 17:55:14 +02:00
public void onResume() {
2017-07-16 19:04:53 +02:00
super.onResume();
2019-09-06 17:55:14 +02:00
if (type != null && type != typeOfSchedule.SERVER) {
2019-01-20 15:20:07 +01:00
//Retrieves scheduled toots
2021-01-19 17:43:51 +01:00
new RetrieveScheduledTootsAsyncTask(context, type, DisplayScheduledTootsFragment.this);
2019-01-20 15:20:07 +01:00
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);
}
2021-01-19 17:43:51 +01:00
warning_battery_message.setOnTouchListener((v, 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;
}
}
2021-01-19 17:43:51 +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
2019-09-06 17:55:14 +02:00
public void onCreate(Bundle saveInstance) {
2017-07-16 17:09:35 +02:00
super.onCreate(saveInstance);
}
@Override
2020-07-04 19:18:44 +02:00
public void onAttach(@NotNull Context context) {
2017-07-16 17:09:35 +02:00
super.onAttach(context);
this.context = context;
}
public void onDestroy() {
super.onDestroy();
}
@Override
public void onRetrieveScheduledToots(List<StoredStatus> storedStatuses) {
mainLoader.setVisibility(View.GONE);
2019-09-06 17:55:14 +02:00
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);
2019-09-06 17:55:14 +02:00
} else {
2017-07-16 17:09:35 +02:00
textviewNoAction.setVisibility(View.VISIBLE);
2019-09-06 17:55:14 +02:00
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
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
}
}
2019-11-15 16:32:25 +01:00
public enum typeOfSchedule {
TOOT,
BOOST,
SERVER
}
2017-07-16 17:09:35 +02:00
}