diff --git a/app/src/main/java/com/hippo/ehviewer/preference/TaskPreference.java b/app/src/main/java/com/hippo/ehviewer/preference/TaskPreference.java new file mode 100644 index 000000000..7891394aa --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/preference/TaskPreference.java @@ -0,0 +1,186 @@ +/* + * Copyright 2016 Hippo Seven + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.hippo.ehviewer.preference; + +import android.app.Dialog; +import android.content.Context; +import android.os.AsyncTask; +import android.os.Parcel; +import android.os.Parcelable; +import android.support.annotation.CallSuper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v7.app.AlertDialog; +import android.util.AttributeSet; + +import com.hippo.ehviewer.EhApplication; +import com.hippo.ehviewer.R; +import com.hippo.preference.DialogPreference; +import com.hippo.yorozuya.IntIdGenerator; + +public abstract class TaskPreference extends DialogPreference { + + private Task mTask; + private int mTaskId = IntIdGenerator.INVALID_ID; + + public TaskPreference(Context context) { + super(context); + } + + public TaskPreference(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public TaskPreference(Context context, AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + @Override + protected void onPrepareDialogBuilder(AlertDialog.Builder builder) { + builder.setTitle(null); + builder.setView(R.layout.preference_dialog_task); + builder.setCancelable(false); + } + + @Override + protected void onDialogCreated(AlertDialog dialog) { + if (null == mTask) { + mTask = onCreateTask(); + mTask.setPreference(this); + mTaskId = ((EhApplication) getContext().getApplicationContext()).putGlobalStuff(mTask); + mTask.execute(); + } + } + + protected void onTaskEnd() { + // Dismiss dialog + Dialog dialog = getDialog(); + if (null != dialog) { + dialog.dismiss(); + } + // Clear async + mTask = null; + mTaskId = IntIdGenerator.INVALID_ID; + } + + @NonNull + protected abstract Task onCreateTask(); + + @Override + public void onActivityDestroy() { + super.onActivityDestroy(); + + if (null != mTask) { + mTask.setPreference(null); + } + } + + @Override + protected Parcelable onSaveInstanceState() { + final Parcelable superState = super.onSaveInstanceState(); + final SavedState myState = new SavedState(superState); + myState.asyncTaskId = mTaskId; + return myState; + } + + @Override + protected void onRestoreInstanceState(Parcelable state) { + if (state == null || !state.getClass().equals(SavedState.class)) { + // Didn't save state for us in onSaveInstanceState + super.onRestoreInstanceState(state); + return; + } + + SavedState myState = (SavedState) state; + mTaskId = myState.asyncTaskId; + if (IntIdGenerator.INVALID_ID != mTaskId) { + Object o = ((EhApplication) getContext().getApplicationContext()).getGlobalStuff(mTaskId); + if (o instanceof Task) { + mTask = (Task) o; + mTask.setPreference(this); + } + } + if (null == mTask) { + mTaskId = IntIdGenerator.INVALID_ID; + } + + // TODO if not task, show not reopen dialog + + super.onRestoreInstanceState(myState.getSuperState()); + } + + private static class SavedState extends BaseSavedState { + int asyncTaskId; + + public SavedState(Parcel source) { + super(source); + asyncTaskId = source.readInt(); + } + + @Override + public void writeToParcel(Parcel dest, int flags) { + super.writeToParcel(dest, flags); + dest.writeInt(asyncTaskId); + } + + public SavedState(Parcelable superState) { + super(superState); + } + + public static final Parcelable.Creator CREATOR = + new Parcelable.Creator() { + @Override + public SavedState createFromParcel(Parcel in) { + return new SavedState(in); + } + + @Override + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + } + + public abstract static class Task extends AsyncTask { + + private final EhApplication mApplication; + @Nullable + private TaskPreference mPreference; + + public Task(@NonNull Context context) { + mApplication = (EhApplication) context.getApplicationContext(); + } + + @Nullable + public TaskPreference getPreference() { + return mPreference; + } + + public void setPreference(@Nullable TaskPreference preference) { + mPreference = preference; + } + + @CallSuper + @Override + protected void onPostExecute(Object o) { + mApplication.removeGlobalStuff(this); + if (null != mPreference) { + mPreference.onTaskEnd(); + } + } + } +} diff --git a/app/src/main/res/layout/preference_dialog_task.xml b/app/src/main/res/layout/preference_dialog_task.xml new file mode 100644 index 000000000..a59347ccb --- /dev/null +++ b/app/src/main/res/layout/preference_dialog_task.xml @@ -0,0 +1,40 @@ + + + + + + + + + + diff --git a/app/src/main/res/values-zh-rCN/strings.xml b/app/src/main/res/values-zh-rCN/strings.xml index 885629073..67e011f98 100644 --- a/app/src/main/res/values-zh-rCN/strings.xml +++ b/app/src/main/res/values-zh-rCN/strings.xml @@ -489,6 +489,8 @@ N/A 其他 + 请稍候 + 启用 已启用 未启用 diff --git a/app/src/main/res/values-zh-rHK/strings.xml b/app/src/main/res/values-zh-rHK/strings.xml index 2e4ef132f..6b464ccdf 100644 --- a/app/src/main/res/values-zh-rHK/strings.xml +++ b/app/src/main/res/values-zh-rHK/strings.xml @@ -489,6 +489,8 @@ N/A 其他 + 請稍候 + 啟用 已啟用 未啟用 diff --git a/app/src/main/res/values-zh-rTW/strings.xml b/app/src/main/res/values-zh-rTW/strings.xml index d684feb1b..ca4a16192 100644 --- a/app/src/main/res/values-zh-rTW/strings.xml +++ b/app/src/main/res/values-zh-rTW/strings.xml @@ -489,6 +489,8 @@ N/A 其他 + 請稍候 + 啟用 已啟用 未啟用 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index ab187b3be..9a1cd8ef8 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -545,6 +545,8 @@ N/A Other + Pleas wait + License