forked from organicmaps/organicmaps
[andorid] Maded OperationStatus, Result and Error parcelable in bookmark package
This commit is contained in:
parent
10ff21d26d
commit
f004ad5757
4 changed files with 127 additions and 16 deletions
|
@ -1,34 +1,73 @@
|
|||
package com.mapswithme.maps.bookmarks;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
public class OperationStatus<R, E>
|
||||
import com.mapswithme.maps.bookmarks.data.Error;
|
||||
import com.mapswithme.maps.bookmarks.data.Result;
|
||||
|
||||
public class OperationStatus implements Parcelable
|
||||
{
|
||||
@Nullable
|
||||
private final R mResult;
|
||||
private final Result mResult;
|
||||
@Nullable
|
||||
private final E mError;
|
||||
private final Error mError;
|
||||
|
||||
OperationStatus(@Nullable R result, @Nullable E error)
|
||||
OperationStatus(@Nullable Result result, @Nullable Error error)
|
||||
{
|
||||
mResult = result;
|
||||
mError = error;
|
||||
}
|
||||
|
||||
private OperationStatus(Parcel in)
|
||||
{
|
||||
mResult = in.readParcelable(Result.class.getClassLoader());
|
||||
mError = in.readParcelable(Error.class.getClassLoader());
|
||||
}
|
||||
|
||||
public static final Creator<OperationStatus> CREATOR = new Creator<OperationStatus>()
|
||||
{
|
||||
@Override
|
||||
public OperationStatus createFromParcel(Parcel in)
|
||||
{
|
||||
return new OperationStatus(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OperationStatus[] newArray(int size)
|
||||
{
|
||||
return new OperationStatus[size];
|
||||
}
|
||||
};
|
||||
|
||||
public boolean isOk()
|
||||
{
|
||||
return mError == null;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public R getResult()
|
||||
public Result getResult()
|
||||
{
|
||||
return mResult;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public E getError()
|
||||
public Error getError()
|
||||
{
|
||||
return mError;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags)
|
||||
{
|
||||
dest.writeParcelable(mResult, flags);
|
||||
dest.writeParcelable(mError, flags);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,12 +40,12 @@ public class SystemDownloadCompletedService extends JobIntentService
|
|||
if (manager == null)
|
||||
throw new IllegalStateException("Failed to get a download manager");
|
||||
|
||||
final OperationStatus<Result, Error> status = doInBackground(manager, intent);
|
||||
final OperationStatus status = doInBackground(manager, intent);
|
||||
UiThread.run(new NotifyCoreTask(getApplicationContext(), status));
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private OperationStatus<Result, Error> doInBackground(@NonNull DownloadManager manager,
|
||||
private OperationStatus doInBackground(@NonNull DownloadManager manager,
|
||||
@NonNull Intent intent)
|
||||
{
|
||||
try
|
||||
|
@ -54,12 +54,12 @@ public class SystemDownloadCompletedService extends JobIntentService
|
|||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
return new OperationStatus<>(null, new Error(e.getMessage()));
|
||||
return new OperationStatus(null, new Error(e.getMessage()));
|
||||
}
|
||||
}
|
||||
|
||||
@NonNull
|
||||
private static OperationStatus<Result, Error> doInBackgroundInternal(
|
||||
private static OperationStatus doInBackgroundInternal(
|
||||
@NonNull DownloadManager manager, @NonNull Intent intent) throws IOException
|
||||
{
|
||||
Cursor cursor = null;
|
||||
|
@ -74,11 +74,11 @@ public class SystemDownloadCompletedService extends JobIntentService
|
|||
if (isDownloadFailed(cursor))
|
||||
{
|
||||
Error error = new Error(getHttpStatus(cursor), getErrorMessage(cursor));
|
||||
return new OperationStatus<>(null, error);
|
||||
return new OperationStatus(null, error);
|
||||
}
|
||||
|
||||
Result result = new Result(getFilePath(cursor), getArchiveId(cursor));
|
||||
return new OperationStatus<>(result, null);
|
||||
return new OperationStatus(result, null);
|
||||
}
|
||||
throw new IOException("Failed to move the cursor at first row");
|
||||
}
|
||||
|
@ -130,10 +130,10 @@ public class SystemDownloadCompletedService extends JobIntentService
|
|||
@NonNull
|
||||
private final Context mAppContext;
|
||||
@NonNull
|
||||
private final OperationStatus<Result, Error> mStatus;
|
||||
private final OperationStatus mStatus;
|
||||
|
||||
private NotifyCoreTask(@NonNull Context applicationContext,
|
||||
@NonNull OperationStatus<Result, Error> status)
|
||||
@NonNull OperationStatus status)
|
||||
{
|
||||
mAppContext = applicationContext;
|
||||
mStatus = status;
|
||||
|
|
|
@ -1,10 +1,12 @@
|
|||
package com.mapswithme.maps.bookmarks.data;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import java.net.HttpURLConnection;
|
||||
|
||||
public class Error
|
||||
public class Error implements Parcelable
|
||||
{
|
||||
private final int mHttpCode;
|
||||
@Nullable
|
||||
|
@ -21,6 +23,12 @@ public class Error
|
|||
this(HttpURLConnection.HTTP_UNAVAILABLE, message);
|
||||
}
|
||||
|
||||
protected Error(Parcel in)
|
||||
{
|
||||
mHttpCode = in.readInt();
|
||||
mMessage = in.readString();
|
||||
}
|
||||
|
||||
public int getHttpCode()
|
||||
{
|
||||
return mHttpCode;
|
||||
|
@ -41,4 +49,32 @@ public class Error
|
|||
{
|
||||
return mHttpCode == HttpURLConnection.HTTP_PAYMENT_REQUIRED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags)
|
||||
{
|
||||
dest.writeInt(mHttpCode);
|
||||
dest.writeString(mMessage);
|
||||
}
|
||||
|
||||
public static final Creator<Error> CREATOR = new Creator<Error>()
|
||||
{
|
||||
@Override
|
||||
public Error createFromParcel(Parcel in)
|
||||
{
|
||||
return new Error(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Error[] newArray(int size)
|
||||
{
|
||||
return new Error[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
package com.mapswithme.maps.bookmarks.data;
|
||||
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
public class Result
|
||||
public class Result implements Parcelable
|
||||
{
|
||||
@Nullable
|
||||
private final String mFilePath;
|
||||
|
@ -15,6 +17,12 @@ public class Result
|
|||
mArchiveId = archiveId;
|
||||
}
|
||||
|
||||
protected Result(Parcel in)
|
||||
{
|
||||
mFilePath = in.readString();
|
||||
mArchiveId = in.readString();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public String getFilePath()
|
||||
{
|
||||
|
@ -26,4 +34,32 @@ public class Result
|
|||
{
|
||||
return mArchiveId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int describeContents()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToParcel(Parcel dest, int flags)
|
||||
{
|
||||
dest.writeString(mFilePath);
|
||||
dest.writeString(mArchiveId);
|
||||
}
|
||||
|
||||
public static final Creator<Result> CREATOR = new Creator<Result>()
|
||||
{
|
||||
@Override
|
||||
public Result createFromParcel(Parcel in)
|
||||
{
|
||||
return new Result(in);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Result[] newArray(int size)
|
||||
{
|
||||
return new Result[size];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue