package com.serenegiant.mediastore;
/*
 * libcommon
 * utility/helper classes for myself
 *
 * Copyright (c) 2014-2021 saki t_saki@serenegiant.com
 *
 * 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.
*/

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;

import com.serenegiant.common.R;
import com.serenegiant.graphics.BitmapHelper;
import com.serenegiant.utils.ThreadPool;

import java.util.concurrent.FutureTask;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

/**
 * 非同期で画像読み込みを行うためのヘルパークラス(Runnableを実装)
 */
public abstract class ThumbnailLoader implements Runnable {
	private static final boolean DEBUG = false; // set false on production
	private static final String TAG = ThumbnailLoader.class.getSimpleName();

	@NonNull
	protected final ThumbnailLoaderDrawable mParent;
	@NonNull
	private final FutureTask<Bitmap> mTask;
	@NonNull
	private final MediaInfo mInfo = new MediaInfo();
	private Bitmap mBitmap;

	/**
	 * コンストラクタ
	 * @param parent
	 */
    public ThumbnailLoader(@NonNull final ThumbnailLoaderDrawable parent) {
    	mParent = parent;
		mTask = new FutureTask<Bitmap>(this, null);
    }

	public long id() {
		return mInfo.id;
	}

	public Uri getUri() {
		return mInfo.getUri();
	}

    /**
	 * start loading
	 * @param info
     */
	public synchronized void startLoad(@NonNull final MediaInfo info) {
		mInfo.set(info);
		mBitmap = null;
		ThreadPool.queueEvent(mTask);
	}

	/**
	 * cancel loading
	 */
	public void cancelLoad() {
		mTask.cancel(true);
	}

	/**
	 * 実際の読み込み処理
	 * @param context
	 * @param info
	 * @param requestWidth
	 * @param requestHeight
	 * @return
	 */
	@Nullable
	protected abstract Bitmap loadThumbnail(
		@NonNull final Context context,
		@NonNull final MediaInfo info,
		final int requestWidth, final int requestHeight);

	/**
	 * 指定したDrawableリソースからビットマップとして画像を取得する
	 * @param drawableRes
	 * @return
	 */
	@NonNull
	protected Bitmap loadDefaultThumbnail(
		@NonNull final Context context,
		@DrawableRes final int drawableRes) {

		return BitmapHelper.fromDrawable(context, drawableRes);
	}

	@Override
	public void run() {
		final MediaInfo info;
		synchronized(this) {
			info = new MediaInfo(mInfo);
		}
		if (!mTask.isCancelled()) {
			mBitmap = loadThumbnail(mParent.getContext(), info,
				mParent.getIntrinsicWidth(), mParent.getIntrinsicHeight());
		}
		if (mTask.isCancelled() || !info.equals(mInfo)) {
			return;	// return without callback
		}
		if (mBitmap == null) {
			mBitmap = loadDefaultThumbnail(mParent.getContext(), R.drawable.ic_error_outline_red_24dp);
		}
		// set callback
		mParent.scheduleSelf(mParent, 0);
	}

	public Bitmap getBitmap() {
		return mBitmap;
	}
}

@@ -1499,16 +1499,27 @@
 Info();%0A
+%09@Nullable%0A
 %09private
@@ -1535,16 +1535,16 @@
 Bitmap;%0A
-
 %0A%09/**%0A%09 
@@ -1767,24 +1767,35 @@
 nfo.id;%0A%09%7D%0A%0A
+%09@Nullable%0A
 %09public Uri 
@@ -1850,21 +1850,16 @@
 %09 * 
-start loading
+%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%81%BF%E9%96%8B%E5%A7%8B%E3%81%99%E3%82%8B
 %0A%09 *
@@ -2034,22 +2034,18 @@
 %09 * 
-cancel loading
+%E8%AA%AD%E3%81%BF%E8%BE%BC%E3%81%BF%E4%B8%AD%E6%96%AD%E8%A6%81%E6%B1%82%E3%81%99%E3%82%8B
 %0A%09 *
@@ -2397,279 +2397,8 @@
 );%0A%0A
-%09/**%0A%09 * %E6%8C%87%E5%AE%9A%E3%81%97%E3%81%9FDrawable%E3%83%AA%E3%82%BD%E3%83%BC%E3%82%B9%E3%81%8B%E3%82%89%E3%83%93%E3%83%83%E3%83%88%E3%83%9E%E3%83%83%E3%83%97%E3%81%A8%E3%81%97%E3%81%A6%E7%94%BB%E5%83%8F%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%0A%09 * @param drawableRes%0A%09 * @return%0A%09 */%0A%09@NonNull%0A%09protected Bitmap loadDefaultThumbnail(%0A%09%09@NonNull final Context context,%0A%09%09@DrawableRes final int drawableRes) %7B%0A%0A%09%09return BitmapHelper.fromDrawable(context, drawableRes);%0A%09%7D%0A%0A
 %09@Ov
@@ -2759,132 +2759,8 @@
 %09%09%7D%0A
-%09%09if (mBitmap == null) %7B%0A%09%09%09mBitmap = loadDefaultThumbnail(mParent.getContext(), R.drawable.ic_error_outline_red_24dp);%0A%09%09%7D%0A
 %09%09//
@@ -2813,16 +2813,69 @@
 0);%0A%09%7D%0A%0A
+%09/**%0A%09 * %E8%AA%AD%E3%81%BF%E8%BE%BC%E3%82%93%E3%81%A0%E3%82%B5%E3%83%A0%E3%83%8D%E3%82%A4%E3%83%AB%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B%0A%09 * @return%0A%09 */%0A%09@Nullable%0A
 %09public 

コメント修正・アノテーション追加、デフォルトのサムネイル(drawable)読み込み処理を削除