summaryrefslogtreecommitdiffstats
path: root/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayScheduledTootsFragment.java
blob: 38cf65f0bd620b40a04afc015568017d4b65162d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package fr.gouv.etalab.mastodon.fragments;
/* 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 <http://www.gnu.org/licenses>. */

import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;
import fr.gouv.etalab.mastodon.R;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask;
import fr.gouv.etalab.mastodon.asynctasks.RetrieveScheduledTootsAsyncTask;
import fr.gouv.etalab.mastodon.client.APIResponse;
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
import fr.gouv.etalab.mastodon.drawers.ScheduledTootsListAdapter;
import fr.gouv.etalab.mastodon.helper.Helper;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveFeedsInterface;
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveScheduledTootsInterface;
import fr.gouv.etalab.mastodon.sqlite.BoostScheduleDAO;
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;

import static fr.gouv.etalab.mastodon.helper.Helper.changeDrawableColor;


/**
 * Created by Thomas on 16/07/2017.
 * Fragment to display scheduled toots
 */
public class DisplayScheduledTootsFragment extends Fragment implements OnRetrieveScheduledTootsInterface, OnRetrieveFeedsInterface {


    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;
    private List<StoredStatus> storedStatuses;
    private boolean firstCall;
    private ScheduledTootsListAdapter scheduledTootsListAdapter;

    public enum typeOfSchedule{
        TOOT,
        BOOST,
        SERVER
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        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;
        lv_scheduled_toots = rootView.findViewById(R.id.lv_scheduled_toots);
        firstCall = true;
        mainLoader = rootView.findViewById(R.id.loader);
        warning_battery_message = rootView.findViewById(R.id.warning_battery_message);
        textviewNoAction = rootView.findViewById(R.id.no_action);
        mainLoader.setVisibility(View.VISIBLE);
        storedStatuses = new ArrayList<>();
        //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();
        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);
        return rootView;
    }

    @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;
    }

    @Override
    public void onResume(){
        super.onResume();
        //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
            changeDrawableColor(context, R.drawable.ic_report, R.color.mastodonC4);
            changeDrawableColor(context, R.drawable.ic_cancel, R.color.mastodonC4);
            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;
                        }
                    }
                    return false;
                }
            });
            warning_battery_message.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("BatteryLife")
                @Override
                public void onClick(View v) {
                    try {
                        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){}
                    }
                }
            });
        }else {
            warning_battery_message.setVisibility(View.GONE);
        }
    }

    @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 ){
            scheduledTootsListAdapter = new ScheduledTootsListAdapter(context, type, storedStatuses, textviewNoAction);
            lv_scheduled_toots.setAdapter(scheduledTootsListAdapter);
            textviewNoAction.setVisibility(View.GONE);
        }else {
            textviewNoAction.setVisibility(View.VISIBLE);
            if( type == typeOfSchedule.BOOST) {