Improves scheduled toots
This commit is contained in:
parent
474aedec50
commit
7bbb2d10ee
|
@ -96,6 +96,14 @@ public class AboutActivity extends AppCompatActivity implements OnRetrieveSearcA
|
|||
startActivity(browserIntent);
|
||||
}
|
||||
});
|
||||
Button about_translation = (Button) findViewById(R.id.about_translation);
|
||||
about_translation.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://translate.yandex.com/"));
|
||||
startActivity(browserIntent);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -176,10 +176,6 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
|
|||
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_action_globe,R.color.dark_text);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_action_camera,R.color.dark_text);
|
||||
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_previous,R.color.dark_text);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_next,R.color.dark_text);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_check,R.color.dark_text);
|
||||
}else {
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_action_globe,R.color.black);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_action_lock_open,R.color.black);
|
||||
|
@ -188,32 +184,10 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
|
|||
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_action_globe,R.color.black);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_action_camera,R.color.black);
|
||||
|
||||
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_previous,R.color.black);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_next,R.color.black);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_check,R.color.black);
|
||||
}
|
||||
|
||||
final LinearLayout drawer_layout = (LinearLayout) findViewById(R.id.drawer_layout);
|
||||
|
||||
/*drawer_layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
int heightDiff = drawer_layout.getRootView().getHeight() - drawer_layout.getHeight();
|
||||
if (heightDiff > 100) {
|
||||
ViewGroup.LayoutParams params = toot_picture_container.getLayoutParams();
|
||||
params.height = (int) Helper.convertDpToPixel(20, getApplicationContext());
|
||||
params.width = (int) Helper.convertDpToPixel(20, getApplicationContext());
|
||||
toot_picture_container.setLayoutParams(params);
|
||||
} else {
|
||||
ViewGroup.LayoutParams params = toot_picture_container.getLayoutParams();
|
||||
params.height = (int) Helper.convertDpToPixel(100, getApplicationContext());
|
||||
params.width = (int) Helper.convertDpToPixel(100, getApplicationContext());
|
||||
toot_picture_container.setLayoutParams(params);
|
||||
}
|
||||
}
|
||||
});*/
|
||||
|
||||
drawer_layout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@Override
|
||||
|
@ -616,6 +590,17 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
|
|||
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(TootActivity.this);
|
||||
LayoutInflater inflater = this.getLayoutInflater();
|
||||
View dialogView = inflater.inflate(R.layout.datetime_picker, null);
|
||||
SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
||||
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
||||
if( theme == Helper.THEME_DARK){
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_previous,R.color.dark_text);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_next,R.color.dark_text);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_check,R.color.dark_text);
|
||||
}else {
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_previous,R.color.black);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_skip_next,R.color.black);
|
||||
changeDrawableColor(TootActivity.this, R.drawable.ic_check,R.color.black);
|
||||
}
|
||||
dialogBuilder.setView(dialogView);
|
||||
final AlertDialog alertDialog = dialogBuilder.create();
|
||||
|
||||
|
@ -678,7 +663,7 @@ public class TootActivity extends AppCompatActivity implements OnRetrieveSearcAc
|
|||
//Store the toot as draft first
|
||||
storeToot(false);
|
||||
//Schedules the toot
|
||||
ScheduledTootsSyncJob.schedule(getApplicationContext(), false, currentToId, time);
|
||||
ScheduledTootsSyncJob.schedule(getApplicationContext(), currentToId, time);
|
||||
//Clear content
|
||||
toot_content.setText("");
|
||||
toot_cw_content.setText("");
|
||||
|
|
|
@ -17,10 +17,18 @@ package fr.gouv.etalab.mastodon.asynctasks;
|
|||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.SystemClock;
|
||||
|
||||
import com.evernote.android.job.JobManager;
|
||||
import com.evernote.android.job.JobRequest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveScheduledTootsInterface;
|
||||
import fr.gouv.etalab.mastodon.jobs.ScheduledTootsSyncJob;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
|
||||
|
||||
|
@ -36,7 +44,6 @@ public class RetrieveScheduledTootsAsyncTask extends AsyncTask<Void, Void, Void>
|
|||
private OnRetrieveScheduledTootsInterface listener;
|
||||
private List<StoredStatus> storedStatuses;
|
||||
|
||||
|
||||
public RetrieveScheduledTootsAsyncTask(Context context, OnRetrieveScheduledTootsInterface onRetrieveScheduledTootsInterface){
|
||||
this.context = context;
|
||||
this.listener = onRetrieveScheduledTootsInterface;
|
||||
|
@ -46,7 +53,32 @@ public class RetrieveScheduledTootsAsyncTask extends AsyncTask<Void, Void, Void>
|
|||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
//Retrieves job asked by the user
|
||||
storedStatuses = new StatusStoredDAO(context, db).getAllScheduled();
|
||||
//Retrieves real jobs still waiting
|
||||
Set<JobRequest> jobRequests = JobManager.instance().getAllJobRequestsForTag(ScheduledTootsSyncJob.SCHEDULED_TOOT);
|
||||
int[] jobIds;
|
||||
if( jobRequests != null && jobRequests.size() > 0 ){
|
||||
int i = 0;
|
||||
jobIds = new int[jobRequests.size()];
|
||||
for(JobRequest jobRequest : jobRequests){
|
||||
jobIds[i] = jobRequest.getJobId();
|
||||
i++;
|
||||
}
|
||||
}else{
|
||||
jobIds = new int[]{};
|
||||
}
|
||||
if( storedStatuses != null && storedStatuses.size() > 0 ){
|
||||
for(StoredStatus ss: storedStatuses){
|
||||
if (!Helper.isJobPresent(jobIds, ss.getJobId())){
|
||||
//JobId is fixed to -1 which means an error occured (it was never sent)
|
||||
new StatusStoredDAO(context, db).updateJobId(ss.getId(),-1);
|
||||
}
|
||||
}
|
||||
//Lets time to update db before dispaying
|
||||
SystemClock.sleep(1000);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
|
|
@ -16,6 +16,7 @@ package fr.gouv.etalab.mastodon.client;
|
|||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.util.Log;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.loopj.android.http.AsyncHttpResponseHandler;
|
||||
|
@ -869,6 +870,7 @@ public class API {
|
|||
public void onFailure(int statusCode, Header[] headers, Throwable error, JSONObject response) {
|
||||
actionCode = statusCode;
|
||||
setError(statusCode, error);
|
||||
error.printStackTrace();
|
||||
}
|
||||
});
|
||||
}else{
|
||||
|
@ -1461,7 +1463,6 @@ public class API {
|
|||
}
|
||||
|
||||
private void post(String action, int timeout, RequestParams params, AsyncHttpResponseHandler responseHandler) {
|
||||
|
||||
try {
|
||||
client.setConnectTimeout(timeout); //10s timeout
|
||||
client.setUserAgent(USER_AGENT);
|
||||
|
|
|
@ -20,28 +20,34 @@ import android.content.DialogInterface;
|
|||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.os.Build;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.widget.CardView;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.BaseAdapter;
|
||||
import android.widget.Button;
|
||||
import android.widget.DatePicker;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.RelativeLayout;
|
||||
import android.widget.TextView;
|
||||
import android.widget.TimePicker;
|
||||
import android.widget.Toast;
|
||||
|
||||
import com.evernote.android.job.Job;
|
||||
import com.evernote.android.job.JobManager;
|
||||
import com.evernote.android.job.JobRequest;
|
||||
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import fr.gouv.etalab.mastodon.activities.MainActivity;
|
||||
import fr.gouv.etalab.mastodon.activities.TootActivity;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Application;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.jobs.ApplicationJob;
|
||||
import fr.gouv.etalab.mastodon.jobs.ScheduledTootsSyncJob;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
|
||||
|
@ -60,13 +66,14 @@ public class ScheduledTootsListAdapter extends BaseAdapter {
|
|||
private List<StoredStatus> storedStatuses;
|
||||
private LayoutInflater layoutInflater;
|
||||
private ScheduledTootsListAdapter scheduledTootsListAdapter;
|
||||
private RelativeLayout textviewNoAction;
|
||||
|
||||
|
||||
public ScheduledTootsListAdapter(Context context, List<StoredStatus> storedStatuses){
|
||||
public ScheduledTootsListAdapter(Context context, List<StoredStatus> storedStatuses, RelativeLayout textviewNoAction){
|
||||
this.context = context;
|
||||
this.storedStatuses = storedStatuses;
|
||||
layoutInflater = LayoutInflater.from(this.context);
|
||||
scheduledTootsListAdapter = this;
|
||||
this.textviewNoAction = textviewNoAction;
|
||||
}
|
||||
|
||||
|
||||
|
@ -143,8 +150,6 @@ public class ScheduledTootsListAdapter extends BaseAdapter {
|
|||
}
|
||||
final SQLiteDatabase db = Sqlite.getInstance(context, Sqlite.DB_NAME, null, Sqlite.DB_VERSION).open();
|
||||
|
||||
final Job job = JobManager.instance().getJob(storedStatus.getJobId());
|
||||
|
||||
//Delete scheduled toot
|
||||
holder.scheduled_toot_delete.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
|
@ -159,6 +164,12 @@ public class ScheduledTootsListAdapter extends BaseAdapter {
|
|||
new StatusStoredDAO(context, db).remove(storedStatus.getId());
|
||||
storedStatuses.remove(storedStatus);
|
||||
scheduledTootsListAdapter.notifyDataSetChanged();
|
||||
if( storedStatuses.size() == 0 && textviewNoAction != null && textviewNoAction.getVisibility() == View.GONE)
|
||||
textviewNoAction.setVisibility(View.VISIBLE);
|
||||
try {
|
||||
//Cancel the job
|
||||
ApplicationJob.cancelJob(storedStatus.getJobId());
|
||||
}catch (Exception ignored){}
|
||||
dialog.dismiss();
|
||||
}
|
||||
})
|
||||
|
@ -172,17 +183,111 @@ public class ScheduledTootsListAdapter extends BaseAdapter {
|
|||
.show();
|
||||
}
|
||||
});
|
||||
if( job == null){
|
||||
|
||||
if (storedStatus.getJobId() > 0) {
|
||||
holder.scheduled_toot_failed.setVisibility(View.GONE);
|
||||
}else {
|
||||
holder.scheduled_toot_failed.setVisibility(View.GONE);
|
||||
holder.scheduled_toot_failed.setVisibility(View.VISIBLE);
|
||||
}
|
||||
holder.scheduled_toot_media_count.setText(context.getString(R.string.media_count, status.getMedia_attachments().size()));
|
||||
holder.scheduled_toot_date_creation.setText(Helper.dateToString(context, storedStatus.getCreation_date()));
|
||||
holder.scheduled_toot_date.setText(Helper.dateToString(context, storedStatus.getScheduled_date()));
|
||||
holder.scheduled_toot_date_creation.setOnClickListener(new View.OnClickListener() {
|
||||
holder.scheduled_toot_date.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
|
||||
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context);
|
||||
LayoutInflater inflater = ((MainActivity)context).getLayoutInflater();
|
||||
View dialogView = inflater.inflate(R.layout.datetime_picker, null);
|
||||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE);
|
||||
int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK);
|
||||
if( theme == Helper.THEME_DARK){
|
||||
changeDrawableColor(context, R.drawable.ic_skip_previous,R.color.dark_text);
|
||||
changeDrawableColor(context, R.drawable.ic_skip_next,R.color.dark_text);
|
||||
changeDrawableColor(context, R.drawable.ic_check,R.color.dark_text);
|
||||
}else {
|
||||
changeDrawableColor(context, R.drawable.ic_skip_previous,R.color.black);
|
||||
changeDrawableColor(context, R.drawable.ic_skip_next,R.color.black);
|
||||
changeDrawableColor(context, R.drawable.ic_check,R.color.black);
|
||||
}
|
||||
dialogBuilder.setView(dialogView);
|
||||
final AlertDialog alertDialog = dialogBuilder.create();
|
||||
|
||||
final DatePicker datePicker = (DatePicker) dialogView.findViewById(R.id.date_picker);
|
||||
final TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker);
|
||||
Button date_time_cancel = (Button) dialogView.findViewById(R.id.date_time_cancel);
|
||||
final ImageButton date_time_previous = (ImageButton) dialogView.findViewById(R.id.date_time_previous);
|
||||
final ImageButton date_time_next = (ImageButton) dialogView.findViewById(R.id.date_time_next);
|
||||
final ImageButton date_time_set = (ImageButton) dialogView.findViewById(R.id.date_time_set);
|
||||
|
||||
//Buttons management
|
||||
date_time_cancel.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
alertDialog.dismiss();
|
||||
}
|
||||
});
|
||||
date_time_next.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
datePicker.setVisibility(View.GONE);
|
||||
timePicker.setVisibility(View.VISIBLE);
|
||||
date_time_previous.setVisibility(View.VISIBLE);
|
||||
date_time_next.setVisibility(View.GONE);
|
||||
date_time_set.setVisibility(View.VISIBLE);
|
||||
}
|
||||
});
|
||||
date_time_previous.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
datePicker.setVisibility(View.VISIBLE);
|
||||
timePicker.setVisibility(View.GONE);
|
||||
date_time_previous.setVisibility(View.GONE);
|
||||
date_time_next.setVisibility(View.VISIBLE);
|
||||
date_time_set.setVisibility(View.GONE);
|
||||
}
|
||||
});
|
||||
date_time_set.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
int hour, minute;
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
|
||||
hour = timePicker.getHour();
|
||||
minute = timePicker.getMinute();
|
||||
}else {
|
||||
//noinspection deprecation
|
||||
hour = timePicker.getCurrentHour();
|
||||
//noinspection deprecation
|
||||
minute = timePicker.getCurrentMinute();
|
||||
}
|
||||
Calendar calendar = new GregorianCalendar(datePicker.getYear(),
|
||||
datePicker.getMonth(),
|
||||
datePicker.getDayOfMonth(),
|
||||
hour,
|
||||
minute);
|
||||
long time = calendar.getTimeInMillis();
|
||||
if( (time - new Date().getTime()) < 60000 ){
|
||||
Toast.makeText(context, R.string.toot_scheduled_date, Toast.LENGTH_LONG).show();
|
||||
}else {
|
||||
//Schedules the toot to the new date
|
||||
try {
|
||||
//Removes the job
|
||||
ApplicationJob.cancelJob(storedStatus.getJobId());
|
||||
//Replace it by the new one
|
||||
ScheduledTootsSyncJob.schedule(context, storedStatus.getId(), time);
|
||||
StoredStatus storedStatusnew = new StatusStoredDAO(context, db).getStatus(storedStatus.getId());
|
||||
//Date displayed is changed
|
||||
storedStatus.setScheduled_date(storedStatusnew.getScheduled_date());
|
||||
scheduledTootsListAdapter.notifyDataSetChanged();
|
||||
//Notifiy all is ok
|
||||
Toast.makeText(context,R.string.toot_scheduled, Toast.LENGTH_LONG).show();
|
||||
}catch (Exception ignored){}
|
||||
alertDialog.dismiss();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
alertDialog.show();
|
||||
}
|
||||
});
|
||||
holder.scheduled_toot_title.setText(status.getContent());
|
||||
|
@ -203,7 +308,6 @@ public class ScheduledTootsListAdapter extends BaseAdapter {
|
|||
return convertView;
|
||||
}
|
||||
|
||||
|
||||
private class ViewHolder {
|
||||
CardView scheduled_toot_container;
|
||||
TextView scheduled_toot_title;
|
||||
|
|
|
@ -24,11 +24,18 @@ import android.view.View;
|
|||
import android.view.ViewGroup;
|
||||
import android.widget.ListView;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
import com.evernote.android.job.JobManager;
|
||||
import com.evernote.android.job.JobRequest;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import fr.gouv.etalab.mastodon.asynctasks.RetrieveScheduledTootsAsyncTask;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
|
||||
import fr.gouv.etalab.mastodon.drawers.ScheduledTootsListAdapter;
|
||||
import fr.gouv.etalab.mastodon.interfaces.OnRetrieveScheduledTootsInterface;
|
||||
import fr.gouv.etalab.mastodon.jobs.ScheduledTootsSyncJob;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
|
||||
import mastodon.etalab.gouv.fr.mastodon.R;
|
||||
|
@ -98,8 +105,9 @@ public class DisplayScheduledTootsFragment extends Fragment implements OnRetriev
|
|||
|
||||
mainLoader.setVisibility(View.GONE);
|
||||
if( storedStatuses != null && storedStatuses.size() > 0 ){
|
||||
ScheduledTootsListAdapter scheduledTootsListAdapter = new ScheduledTootsListAdapter(context, storedStatuses);
|
||||
ScheduledTootsListAdapter scheduledTootsListAdapter = new ScheduledTootsListAdapter(context, storedStatuses, textviewNoAction);
|
||||
lv_scheduled_toots.setAdapter(scheduledTootsListAdapter);
|
||||
textviewNoAction.setVisibility(View.GONE);
|
||||
}else {
|
||||
textviewNoAction.setVisibility(View.VISIBLE);
|
||||
}
|
||||
|
|
|
@ -1229,13 +1229,36 @@ public class Helper {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Serialized a Status class
|
||||
* @param status Status to serialize
|
||||
* @return String serialized Status
|
||||
*/
|
||||
public static String statusToStringStorage(Status status){
|
||||
Gson gson = new Gson();
|
||||
return gson.toJson(status);
|
||||
}
|
||||
|
||||
/**
|
||||
* Unserialized a Status
|
||||
* @param serializedStatus String serialized status
|
||||
* @return Status
|
||||
*/
|
||||
public static Status restoreStatusFromString(String serializedStatus){
|
||||
Gson gson = new Gson();
|
||||
return gson.fromJson(serializedStatus, Status.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a job id is in array of ids
|
||||
* @param jobIds int[]
|
||||
* @param id int id to check
|
||||
* @return boolean
|
||||
*/
|
||||
public static boolean isJobPresent(int[] jobIds, int id){
|
||||
for(int x:jobIds) {
|
||||
if (x == id) {return true;}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ public class ApplicationJob implements JobCreator {
|
|||
JobManager.instance().cancelAllForTag(TAG);
|
||||
}
|
||||
|
||||
private void cancelJob(int jobId) {
|
||||
public static void cancelJob(int jobId) {
|
||||
JobManager.instance().cancel(jobId);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,6 +17,8 @@ package fr.gouv.etalab.mastodon.jobs;
|
|||
import android.content.Context;
|
||||
import android.database.sqlite.SQLiteDatabase;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.util.Log;
|
||||
|
||||
import com.evernote.android.job.Job;
|
||||
import com.evernote.android.job.JobRequest;
|
||||
import java.util.Date;
|
||||
|
@ -26,6 +28,7 @@ import fr.gouv.etalab.mastodon.client.API;
|
|||
import fr.gouv.etalab.mastodon.client.Entities.Account;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.Status;
|
||||
import fr.gouv.etalab.mastodon.client.Entities.StoredStatus;
|
||||
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||
import fr.gouv.etalab.mastodon.sqlite.AccountDAO;
|
||||
import fr.gouv.etalab.mastodon.sqlite.Sqlite;
|
||||
import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
|
||||
|
@ -38,7 +41,7 @@ import fr.gouv.etalab.mastodon.sqlite.StatusStoredDAO;
|
|||
|
||||
public class ScheduledTootsSyncJob extends Job {
|
||||
|
||||
static final String SCHEDULED_TOOT = "job_scheduled_toot";
|
||||
public static final String SCHEDULED_TOOT = "job_scheduled_toot";
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
|
@ -52,7 +55,7 @@ public class ScheduledTootsSyncJob extends Job {
|
|||
String userId = storedStatus.getUserId();
|
||||
String instance = storedStatus.getInstance();
|
||||
if( instance != null && userId != null){
|
||||
Account account = new AccountDAO(getContext(), db).getAccountByUserIDInstance(userId, instance);
|
||||
Account account = new AccountDAO(getContext(), db).getAccountByUserIDInstance(userId, instance);
|
||||
if( account != null){
|
||||
//Retrieves the linked status to toot
|
||||
Status status = storedStatus.getStatus();
|
||||
|
@ -70,7 +73,7 @@ public class ScheduledTootsSyncJob extends Job {
|
|||
}
|
||||
|
||||
|
||||
public static int schedule(Context context, boolean updateCurrent, long id, long timestampScheduling){
|
||||
public static int schedule(Context context, long id, long timestampScheduling){
|
||||
|
||||
long startMs = (timestampScheduling - new Date().getTime());
|
||||
long endMs = startMs + TimeUnit.MINUTES.toMillis(5);
|
||||
|
@ -79,7 +82,7 @@ public class ScheduledTootsSyncJob extends Job {
|
|||
int jobId = new JobRequest.Builder(ScheduledTootsSyncJob.SCHEDULED_TOOT)
|
||||
.setExecutionWindow(startMs, endMs)
|
||||
.setPersisted(true)
|
||||
.setUpdateCurrent(updateCurrent)
|
||||
.setUpdateCurrent(false)
|
||||
.setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
|
||||
.setRequirementsEnforced(false)
|
||||
.build()
|
||||
|
|
|
@ -141,7 +141,7 @@ public class AccountDAO {
|
|||
*/
|
||||
public Account getAccountByUserIDInstance(String accountId, String instance){
|
||||
try {
|
||||
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = '" + accountId + "' AND " + Sqlite.COL_INSTANCE + "= '"+ Sqlite.COL_INSTANCE +"'", null, null, null, null, "1");
|
||||
Cursor c = db.query(Sqlite.TABLE_USER_ACCOUNT, null, Sqlite.COL_USER_ID + " = '" + accountId + "' AND " + Sqlite.COL_INSTANCE + "= '"+ instance +"'", null, null, null, null, "1");
|
||||
return cursorToUser(c);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
|
|
|
@ -96,6 +96,19 @@ public class StatusStoredDAO {
|
|||
new String[]{String.valueOf(id)});
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a Status in database
|
||||
* @param id long
|
||||
* @param jobId int
|
||||
* @return int
|
||||
*/
|
||||
public int updateJobId(long id, int jobId) {
|
||||
ContentValues values = new ContentValues();
|
||||
values.put(Sqlite.COL_IS_SCHEDULED, jobId);
|
||||
return db.update(Sqlite.TABLE_STATUSES_STORED,
|
||||
values, Sqlite.COL_ID + " = ? ",
|
||||
new String[]{String.valueOf(id)});
|
||||
}
|
||||
|
||||
/**
|
||||
* Schedule a status in db
|
||||
|
@ -159,7 +172,7 @@ public class StatusStoredDAO {
|
|||
}
|
||||
|
||||
public int removeAllSent(){
|
||||
return db.delete(Sqlite.TABLE_STATUSES_STORED, Sqlite.COL_IS_SCHEDULED + " > 0 AND " + Sqlite.COL_SENT + " = 1", null);
|
||||
return db.delete(Sqlite.TABLE_STATUSES_STORED, Sqlite.COL_IS_SCHEDULED + " != 0 AND " + Sqlite.COL_SENT + " = 1", null);
|
||||
}
|
||||
|
||||
//------- GETTERS -------
|
||||
|
@ -206,7 +219,7 @@ public class StatusStoredDAO {
|
|||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
||||
String instance = Helper.getLiveInstance(context);
|
||||
Cursor c = db.query(Sqlite.TABLE_STATUSES_STORED, null, Sqlite.COL_USER_ID + " = '" + userId+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " + Sqlite.COL_IS_SCHEDULED + " > 0 AND " + Sqlite.COL_SENT + " = 0", null, null, null, Sqlite.COL_DATE_SCHEDULED + " ASC", null);
|
||||
Cursor c = db.query(Sqlite.TABLE_STATUSES_STORED, null, Sqlite.COL_USER_ID + " = '" + userId+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " + Sqlite.COL_IS_SCHEDULED + " != 0 AND " + Sqlite.COL_SENT + " = 0", null, null, null, Sqlite.COL_DATE_SCHEDULED + " ASC", null);
|
||||
return cursorToListStatuses(c);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
|
@ -221,7 +234,7 @@ public class StatusStoredDAO {
|
|||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
||||
String instance = Helper.getLiveInstance(context);
|
||||
Cursor c = db.query(Sqlite.TABLE_STATUSES_STORED, null, Sqlite.COL_USER_ID + " = '" + userId+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " +Sqlite.COL_IS_SCHEDULED + " = 1 AND " + Sqlite.COL_SENT + " = 0", null, null, null, Sqlite.COL_DATE_CREATION + " DESC", null);
|
||||
Cursor c = db.query(Sqlite.TABLE_STATUSES_STORED, null, Sqlite.COL_USER_ID + " = '" + userId+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " +Sqlite.COL_IS_SCHEDULED + " != 0 AND " + Sqlite.COL_SENT + " = 0", null, null, null, Sqlite.COL_DATE_CREATION + " DESC", null);
|
||||
return cursorToListStatuses(c);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
|
@ -237,7 +250,7 @@ public class StatusStoredDAO {
|
|||
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
|
||||
String userId = sharedpreferences.getString(Helper.PREF_KEY_ID, null);
|
||||
String instance = Helper.getLiveInstance(context);
|
||||
Cursor c = db.query(Sqlite.TABLE_STATUSES_STORED, null, Sqlite.COL_USER_ID + " = '" + userId+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " +Sqlite.COL_IS_SCHEDULED + " = 1 AND " + Sqlite.COL_SENT + " = 1", null, null, null, Sqlite.COL_DATE_CREATION + " DESC", null);
|
||||
Cursor c = db.query(Sqlite.TABLE_STATUSES_STORED, null, Sqlite.COL_USER_ID + " = '" + userId+ "' AND " + Sqlite.COL_INSTANCE + " = '" + instance+ "' AND " +Sqlite.COL_IS_SCHEDULED + " != 0 AND " + Sqlite.COL_SENT + " = 1", null, null, null, Sqlite.COL_DATE_CREATION + " DESC", null);
|
||||
return cursorToListStatuses(c);
|
||||
} catch (Exception e) {
|
||||
return null;
|
||||
|
@ -292,6 +305,8 @@ public class StatusStoredDAO {
|
|||
storedStatus.setCreation_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_CREATION))));
|
||||
storedStatus.setScheduled_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_SCHEDULED))));
|
||||
storedStatus.setSent_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_SENT))));
|
||||
storedStatus.setUserId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
|
||||
storedStatus.setInstance(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
|
||||
//Close the cursor
|
||||
c.close();
|
||||
//Stored status is returned
|
||||
|
@ -319,6 +334,8 @@ public class StatusStoredDAO {
|
|||
storedStatus.setCreation_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_CREATION))));
|
||||
storedStatus.setScheduled_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_SCHEDULED))));
|
||||
storedStatus.setSent_date(Helper.stringToDate(context, c.getString(c.getColumnIndex(Sqlite.COL_DATE_SENT))));
|
||||
storedStatus.setUserId(c.getString(c.getColumnIndex(Sqlite.COL_USER_ID)));
|
||||
storedStatus.setInstance(c.getString(c.getColumnIndex(Sqlite.COL_INSTANCE)));
|
||||
storedStatuses.add(storedStatus);
|
||||
}
|
||||
//Close the cursor
|
||||
|
|
Loading…
Reference in New Issue