From 5cd5ac59d0efc7cfa8e004a5ac61b9de2fe316fd Mon Sep 17 00:00:00 2001 From: 18561577093 <17882492@qq.com> Date: Wed, 18 Aug 2021 09:46:09 +0800 Subject: [PATCH] [AD] 00026 [describe] add lessons cache function [Submitter] wanglixue --- .../app/src/main/AndroidManifest.xml | 10 +- .../himindspore/comment/Constant.java | 10 + .../mindspore/himindspore/comment/Tools.java | 12 +- .../himindspore/server/DownloadServer.java | 79 ++++++++ .../server/DownloadVideoResultEvent.java | 27 +++ .../ui/lessons/LessonsActivity.java | 94 +++++++++- .../himindspore/ui/me/CacheActivity.java | 39 +++- .../himindspore/ui/me/PersonalFragment.java | 19 +- .../himindspore/ui/me/PlayVideoActivity.java | 176 ++++++++++++++++++ .../ui/me/adapter/CacheAdapter.java | 58 ++++-- .../ui/me/adapter/ServerInfoAdapter.java | 6 +- .../ui/me/bean/ServerInfoBean.java | 19 ++ .../src/main/res/layout/activity_lessons.xml | 30 ++- .../main/res/layout/activity_play_video.xml | 14 ++ .../main/res/layout/adapter_layout_cache.xml | 49 +++-- .../app/src/main/res/values/strings.xml | 9 + .../mindspore/common/config/StorageUtils.java | 20 +- .../com/mindspore/common/http/HttpClient.java | 5 + .../com/mindspore/customview/ui/MyToast.java | 4 +- .../dance/src/main/res/values/strings.xml | 1 + 20 files changed, 616 insertions(+), 65 deletions(-) create mode 100644 model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/comment/Constant.java create mode 100644 model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/server/DownloadServer.java create mode 100644 model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/server/DownloadVideoResultEvent.java create mode 100644 model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PlayVideoActivity.java create mode 100644 model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_play_video.xml diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/AndroidManifest.xml b/model_zoo/official/lite/MindSpore_inhand/app/src/main/AndroidManifest.xml index 643bfcd88b..a67a2463c9 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/AndroidManifest.xml +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/AndroidManifest.xml @@ -45,6 +45,11 @@ + @@ -91,7 +96,10 @@ android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize|uiMode" android:screenOrientation="portrait" android:theme="@style/Theme.AppCompat.NoActionBar" /> - + + 0)) { + int dot = filename.lastIndexOf('.'); + if ((dot > -1) && (dot < (filename.length() - 1))) { + return filename.substring(dot + 1); + } + } + return filename; + } } diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/server/DownloadServer.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/server/DownloadServer.java new file mode 100644 index 0000000000..0a27686ba9 --- /dev/null +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/server/DownloadServer.java @@ -0,0 +1,79 @@ +package com.mindspore.himindspore.server; + +import android.content.Context; +import android.content.Intent; +import android.util.Log; + +import androidx.annotation.NonNull; +import androidx.core.app.JobIntentService; + +import com.mindspore.common.config.StorageUtils; +import com.mindspore.common.http.HttpClient; +import com.mindspore.common.net.FileDownLoadObserver; +import com.mindspore.common.netbean.response.CourseDetailsBean; +import com.mindspore.customview.ui.MyToast; +import com.mindspore.himindspore.R; +import com.mindspore.himindspore.comment.Constant; + +import org.greenrobot.eventbus.EventBus; + +import java.io.File; + +import io.reactivex.android.schedulers.AndroidSchedulers; +import io.reactivex.schedulers.Schedulers; + +public class DownloadServer extends JobIntentService { + private static final String TAG = DownloadServer.class.getSimpleName(); + + /** + * 这个Service 唯一的id + */ + static final int JOB_ID = 10111; + + /** + * Convenience method for enqueuing work in to this service. + */ + public static void enqueueWork(Context context, Intent work) { + enqueueWork(context, DownloadServer.class, JOB_ID, work); + } + + + @Override + protected void onHandleWork(@NonNull Intent intent) { + CourseDetailsBean.PostEntityListBean currentVideo = + new CourseDetailsBean.PostEntityListBean(); + currentVideo.setVideoUrl(intent.getStringExtra("videoUrl")); + currentVideo.setTitle(intent.getStringExtra("title")); + downloadVideo(currentVideo, new FileDownLoadObserver() { + @Override + public void onDownLoadSuccess(File file) { + MyToast.MakeText(getResources().getString(R.string.cache_data_download_complete), 1000); + EventBus.getDefault().post(new DownloadVideoResultEvent(currentVideo.getTitle(), + Constant.CACHE_STATE_DOWNLOAD_SUCCESS)); + } + + @Override + public void onDownLoadFail(Throwable throwable) { + MyToast.MakeText(getResources().getString(R.string.cache_data_download_fail), 1000); + EventBus.getDefault().post(new DownloadVideoResultEvent(currentVideo.getTitle(), + Constant.CACHE_STATE_DOWNLOAD_FAIL)); + } + + @Override + public void onProgress(final int progress, long total) { + } + }); + } + + private void downloadVideo(CourseDetailsBean.PostEntityListBean currentVideo, + FileDownLoadObserver fileDownLoadObserver) { + HttpClient.Builder.getAPPService().downloadVideo(currentVideo.getVideoUrl()) + .subscribeOn(Schedulers.io()) + .observeOn(Schedulers.io()) + .observeOn(Schedulers.computation()) + .map(responseBody -> fileDownLoadObserver.saveFile(responseBody, + StorageUtils.VIDEO_DOWNLOAD_PATH, currentVideo.getTitle())) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe(fileDownLoadObserver); + } +} \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/server/DownloadVideoResultEvent.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/server/DownloadVideoResultEvent.java new file mode 100644 index 0000000000..19aeb19e41 --- /dev/null +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/server/DownloadVideoResultEvent.java @@ -0,0 +1,27 @@ +package com.mindspore.himindspore.server; + +public class DownloadVideoResultEvent { + String title; + int result; + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getResult() { + return result; + } + + public void setResult(int result) { + this.result = result; + } + + public DownloadVideoResultEvent(String title, int result) { + this.title = title; + this.result = result; + } +} diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/lessons/LessonsActivity.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/lessons/LessonsActivity.java index e6053b9f7f..1e9ee1e222 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/lessons/LessonsActivity.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/lessons/LessonsActivity.java @@ -1,6 +1,6 @@ package com.mindspore.himindspore.ui.lessons; -import android.annotation.SuppressLint; +import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; @@ -18,12 +18,16 @@ import com.google.android.material.tabs.TabLayout; import com.mindspore.common.base.adapter.BasePagerAdapter; import com.mindspore.common.base.bean.BaseResponseBean; import com.mindspore.common.base.mvp.BaseActivity; +import com.mindspore.common.config.StorageUtils; import com.mindspore.common.exception.ErrorStatus; import com.mindspore.common.netbean.response.CourseDetailsBean; import com.mindspore.customview.ui.MyToast; import com.mindspore.himindspore.R; import com.mindspore.himindspore.bean.ShareBean; +import com.mindspore.himindspore.comment.Constant; import com.mindspore.himindspore.comment.Tools; +import com.mindspore.himindspore.server.DownloadServer; +import com.mindspore.himindspore.server.DownloadVideoResultEvent; import com.mindspore.himindspore.ui.courses.mvp.CourseContract; import com.mindspore.himindspore.ui.courses.mvp.CourseDetailsPresenter; import com.mindspore.himindspore.ui.lessons.event.CurrentVideoChangedEvent; @@ -41,10 +45,12 @@ import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; import org.jetbrains.annotations.NotNull; +import java.io.File; import java.util.ArrayList; import java.util.List; public class LessonsActivity extends BaseActivity implements CourseContract.CourseDetailsView, View.OnClickListener { + LessonGSYVideoPlayer videoPlayer; private boolean isPlay; private boolean isPause; @@ -53,10 +59,12 @@ public class LessonsActivity extends BaseActivity implem private ViewPager viewPager; private TextView beginTv; private int videoPlayState = GSYVideoView.CURRENT_STATE_NORMAL; - + private GSYVideoOptionBuilder gsyVideoOption; private boolean isRefresh = false; private SmartRefreshLayout smartRefreshLayout; private MultipleStatusView multipleStatusView; + TextView mCacheText; + int cacheState = Constant.CACHE_STATE_NO_DOWNLOAD; private static String TAG = LessonsActivity.class.getSimpleName(); @@ -88,6 +96,26 @@ public class LessonsActivity extends BaseActivity implem runOnUiThread(() -> refreshVideo()); } + @Subscribe(threadMode = ThreadMode.MAIN) + public void onEventMainThread(DownloadVideoResultEvent event) { + int result = event.getResult(); + String title = event.getTitle(); + runOnUiThread(() -> { + if (title.equals(getVideoFileName(currentVideo))) { + if (result == Constant.CACHE_STATE_DOWNLOAD_SUCCESS) { + + } else if (result == Constant.CACHE_STATE_DOWNLOAD_FAIL) { + File f = new File(StorageUtils.VIDEO_DOWNLOAD_PATH + + getVideoFileName(currentVideo)); + if (f.exists()) { + f.delete(); + } + } + updateCacheView(); + } + }); + } + public void init() { // 在 OnCreate 中调用 id = getIntent().getIntExtra(ITEM_ID, 0); videoPlayer = findViewById(R.id.player); @@ -99,9 +127,15 @@ public class LessonsActivity extends BaseActivity implem LinearLayout mBulletChat = findViewById(R.id.bullet_chat_layout); LinearLayout mStoreLayout = findViewById(R.id.store_layout); LinearLayout mForwardLayout = findViewById(R.id.forward_layout); + LinearLayout mCacheLayout = findViewById(R.id.cache_layout); + mCacheText = findViewById(R.id.cache_title); + mBulletChat.setOnClickListener(LessonsActivity.this); mStoreLayout.setOnClickListener(LessonsActivity.this); mForwardLayout.setOnClickListener(LessonsActivity.this); + mCacheLayout.setOnClickListener(LessonsActivity.this); + + // 获取网络数据 presenter = new CourseDetailsPresenter(this); presenter.requestDetailsDetailsData(id); @@ -166,7 +200,7 @@ public class LessonsActivity extends BaseActivity implem } private void initVideo(VideoBean videoBean) { - GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder(); + gsyVideoOption = new GSYVideoOptionBuilder(); gsyVideoOption.setThumbImageView(videoBean.getThumbnailView()) .setIsTouchWiget(true) .setRotateViewAuto(false) @@ -245,6 +279,30 @@ public class LessonsActivity extends BaseActivity implem videoBean = new VideoBean(imageView, currentVideo.getVideoUrl(), currentVideo.getTitle()); initPlayer(); playVideo(); + updateCacheView(); + } + + private void updateCacheView() { + if (checkVideoCacheExist()) { // 已经缓存过了 + mCacheText.setText(getResources().getText(R.string.lesson_cache_downloaded)); + cacheState = Constant.CACHE_STATE_DOWNLOADED; + } else { // 未缓存 + mCacheText.setText(getResources().getText(R.string.lesson_cache)); + cacheState = Constant.CACHE_STATE_NO_DOWNLOAD; + } + } + + private boolean checkVideoCacheExist() { + File f = new File(StorageUtils.VIDEO_DOWNLOAD_PATH + getVideoFileName(currentVideo)); + return f.exists(); + } + + private String getVideoFileName(CourseDetailsBean.PostEntityListBean video) { + if (video != null) { + return video.getTitle() + "." + Tools.getExtensionName(video.getVideoUrl()); + } else { + return ""; + } } private void initTabLayout() { @@ -349,9 +407,6 @@ public class LessonsActivity extends BaseActivity implem @Override public void showError(String msg, int errorCode) { - smartRefreshLayout.finishRefresh(); - smartRefreshLayout.finishLoadMore(); - smartRefreshLayout.setEnableLoadMore(false); if (errorCode == ErrorStatus.NETWORK_ERROR) { multipleStatusView.showNoNetwork(); } else { @@ -377,11 +432,38 @@ public class LessonsActivity extends BaseActivity implem .build()); } break; + case R.id.cache_layout: + downloadVideo(); + break; default: throw new IllegalStateException("Unexpected value: " + view.getId()); } } + private void downloadVideo() { + if (cacheState == Constant.CACHE_STATE_DOWNLOADING) { + MyToast.MakeText(getResources().getString(R.string.cache_data_downloading), 1000); + } else if (cacheState == Constant.CACHE_STATE_DOWNLOADED) { + MyToast.MakeText(getResources().getString(R.string.lesson_cache_downloaded), 1000); + } else if (cacheState == Constant.CACHE_STATE_NO_DOWNLOAD) { + if (currentVideo != null) { + // 实现下载此url的视频, + String url = currentVideo.getVideoUrl(); + Intent intent = new Intent(); + intent.putExtra("title", getVideoFileName(currentVideo)); + intent.putExtra("videoUrl", currentVideo.getVideoUrl()); + DownloadServer.enqueueWork(this, intent); + + cacheState = Constant.CACHE_STATE_DOWNLOADING; + mCacheText.setText(getResources().getText(R.string.lesson_cache_downloading)); + } else { + MyToast.MakeText(getResources().getString(R.string.no_cache_data), 1000); + } + } else { + // cacheState error, do nothing. + } + } + private static class VideoBean { View thumbnailView; String videoUrl; diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/CacheActivity.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/CacheActivity.java index d120ce053b..168b67c067 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/CacheActivity.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/CacheActivity.java @@ -23,11 +23,14 @@ import android.widget.FrameLayout; import com.classic.common.MultipleStatusView; import com.mindspore.common.base.mvp.BaseActivity; +import com.mindspore.common.config.StorageUtils; import com.mindspore.customview.ui.AppTitleView; +import com.mindspore.customview.ui.MyToast; import com.mindspore.himindspore.R; import com.mindspore.himindspore.ui.me.adapter.CacheAdapter; import com.scwang.smart.refresh.layout.SmartRefreshLayout; +import java.io.File; import java.util.ArrayList; public class CacheActivity extends BaseActivity { @@ -37,7 +40,7 @@ public class CacheActivity extends BaseActivity { private SmartRefreshLayout smartRefreshLayout; private MultipleStatusView multipleStatusView; private RecyclerView mRecyclerView; - private ArrayList mList = new ArrayList(); + private ArrayList mList = new ArrayList(); protected void init() { appTitle = findViewById(R.id.refreshLayoutTitle); @@ -47,13 +50,45 @@ public class CacheActivity extends BaseActivity { multipleStatusView = findViewById(R.id.multipleStatusView); mRecyclerView = findViewById(R.id.recyclerView); mFrameLayout = findViewById(R.id.fragmentRefreshLayout); + initListData(); CacheAdapter adapter = new CacheAdapter(CacheActivity.this, mList); - Log.d("list", mList.size() + ""); LinearLayoutManager manager = new LinearLayoutManager(CacheActivity.this, LinearLayoutManager.VERTICAL, false); mRecyclerView.setLayoutManager(manager); mRecyclerView.setAdapter(adapter); } + private void initListData() { + File path = new File(StorageUtils.VIDEO_DOWNLOAD_PATH); + File[] files = path.listFiles();// 读取文件夹下文件 + if (files == null || files.length == 0) { + MyToast.MakeText(getResources().getString(R.string.cache_data_list_null), 2000); + return; + } + for (File file : files) { + String name = file.getName(); + int i = name.lastIndexOf('.'); + if (i != -1) { + name = name.substring(i); + if (name.equalsIgnoreCase(".mp4") || name.equalsIgnoreCase(".3gp") + || name.equalsIgnoreCase(".wmv") + || name.equalsIgnoreCase(".ts") || name.equalsIgnoreCase(".rmvb") + || name.equalsIgnoreCase(".mov") || name.equalsIgnoreCase(".m4v") + || name.equalsIgnoreCase(".avi") || name.equalsIgnoreCase(".m3u8") + || name.equalsIgnoreCase(".3gpp") || name.equalsIgnoreCase(".3gpp2") + || name.equalsIgnoreCase(".mkv") || name.equalsIgnoreCase(".flv") + || name.equalsIgnoreCase(".divx") || name.equalsIgnoreCase(".f4v") + || name.equalsIgnoreCase(".rm") || name.equalsIgnoreCase(".asf") + || name.equalsIgnoreCase(".ram") || name.equalsIgnoreCase(".mpg") + || name.equalsIgnoreCase(".v8") || name.equalsIgnoreCase(".swf") + || name.equalsIgnoreCase(".m2v") || name.equalsIgnoreCase(".asx") + || name.equalsIgnoreCase(".ra") || name.equalsIgnoreCase(".ndivx") + || name.equalsIgnoreCase(".xvid")) { + mList.add(file); + } + } + } + } + @Override public int getLayout() { return R.layout.activity_refresh_layout_title; diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PersonalFragment.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PersonalFragment.java index 3248e933c3..2299d54ba0 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PersonalFragment.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PersonalFragment.java @@ -97,8 +97,22 @@ public class PersonalFragment extends BaseFragment im view.findViewById(id.rl_me_version).setOnClickListener(this); mRecyclerView = view.findViewById(id.me_recycler); //推荐服务列表数据,加载位置可自行调换 - infoList.add(new ServerInfoBean(getActivity().getResources().getString(string.per_curriculum), getActivity().getResources().getDrawable(drawable.per_curriculum))); - infoList.add(new ServerInfoBean(getActivity().getResources().getString(string.per_cache), getActivity().getResources().getDrawable(drawable.per_cache))); + ServerInfoBean myCourseBean = new ServerInfoBean(getActivity().getResources() + .getString(string.per_curriculum), + getActivity().getResources().getDrawable(drawable.per_curriculum)); + myCourseBean.setClickListener(v -> { + MyToast.FunctionNoOpen(); + }); + + ServerInfoBean cacheBean = new ServerInfoBean(getActivity().getResources() + .getString(string.per_cache), + getActivity().getResources().getDrawable(drawable.per_cache)); + cacheBean.setClickListener(v -> { + startActivity(new Intent(getActivity(), CacheActivity.class)); + }); + + infoList.add(myCourseBean); + infoList.add(cacheBean); infoList.add(new ServerInfoBean(getActivity().getResources().getString(string.per_problem), getActivity().getResources().getDrawable(drawable.per_award_feedback))); infoList.add(new ServerInfoBean(getActivity().getResources().getString(string.per_gitee), getActivity().getResources().getDrawable(drawable.gitee))); infoList.add(new ServerInfoBean(getActivity().getResources().getString(string.per_official), getActivity().getResources().getDrawable(drawable.per_official))); @@ -107,7 +121,6 @@ public class PersonalFragment extends BaseFragment im mRecyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 4)); infoAdapter = new ServerInfoAdapter(getActivity(), infoList); mRecyclerView.setAdapter(infoAdapter); - infoAdapter.ServerInfoClickListener(this); showPackageInfo(); } diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PlayVideoActivity.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PlayVideoActivity.java new file mode 100644 index 0000000000..ee387b944e --- /dev/null +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/PlayVideoActivity.java @@ -0,0 +1,176 @@ +/** + * Copyright 2021 Huawei Technologies Co., Ltd + *

+ * 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.mindspore.himindspore.ui.me; + + +import android.content.res.Configuration; +import android.view.View; + +import com.mindspore.common.base.mvp.BaseActivity; +import com.mindspore.himindspore.R; +import com.mindspore.himindspore.ui.lessons.LessonGSYVideoPlayer; + +import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder; +import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack; +import com.shuyu.gsyvideoplayer.listener.LockClickListener; +import com.shuyu.gsyvideoplayer.utils.OrientationUtils; +import com.shuyu.gsyvideoplayer.video.base.GSYVideoView; + +import org.jetbrains.annotations.NotNull; + +public class PlayVideoActivity extends BaseActivity { + + LessonGSYVideoPlayer videoPlayer; + String filePath; + String fileTitle; + OrientationUtils orientationUtils; + private GSYVideoOptionBuilder gsyVideoOption; + private boolean isPlay; + private boolean isPause; + private int videoPlayState = GSYVideoView.CURRENT_STATE_NORMAL; + + protected void init() { + videoPlayer = findViewById(R.id.player); + filePath = getIntent().getStringExtra("filePath"); + fileTitle = filePath.substring(filePath.lastIndexOf('/')); + + initPlayer(); + playVideo(); + } + + private void playVideo() { + videoPlayer.startPlayLogic(); + } + + private void initPlayer() { + videoPlayer.setUp(filePath, true, fileTitle); + // 增加title + videoPlayer.getTitleTextView().setVisibility(View.GONE); + //设置返回键 + videoPlayer.getBackButton().setVisibility(View.VISIBLE); + //设置返回按键功能 + videoPlayer.getBackButton().setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + onBackPressed(); + } + }); + // 外部辅助的旋转,帮助全屏 + orientationUtils = new OrientationUtils(this, videoPlayer); + // 初始化不打开外部的旋转 + orientationUtils.setEnable(false); + initVideo(); + videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + //直接横屏 + orientationUtils.resolveByClick(); + //第一个true是否需要隐藏actionbar,第二个true是否需要隐藏statusBar + videoPlayer.startWindowFullscreen(PlayVideoActivity.this, true, true); + } + }); + } + + private void initVideo() { + gsyVideoOption = new GSYVideoOptionBuilder(); + gsyVideoOption.setThumbImageView(new View(this)) + .setIsTouchWiget(true) + .setRotateViewAuto(false) + .setLockLand(false) + .setAutoFullWithSize(false) + .setShowFullAnimation(false) + .setNeedLockFull(true) + .setUrl(filePath) + .setCacheWithPlay(true) + .setVideoTitle(fileTitle) + .setVideoAllCallBack(new GSYSampleCallBack() { + @Override + public void onPrepared(String url, Object... objects) { + super.onPrepared(url, objects); + // 开始播放了才能旋转和全屏 + orientationUtils.setEnable(videoPlayer.isRotateWithSystem()); + isPlay = true; + } + + @Override + public void onPlayError(String url, Object... objects) { + super.onPlayError(url, objects); + } + + @Override + public void onQuitFullscreen(String url, Object... objects) { + super.onQuitFullscreen(url, objects); + if (orientationUtils != null) { + orientationUtils.backToProtVideo(); + } + } + }).setLockClickListener(new LockClickListener() { + @Override + public void onClick(View view, boolean lock) { + if (orientationUtils != null) { + //配合下方的onConfigurationChanged + orientationUtils.setEnable(!lock); + } + } + }).build(videoPlayer); + } + + @Override + public int getLayout() { + return R.layout.activity_play_video; + } + + @Override + protected void onPause() { + super.onPause(); + videoPlayState = videoPlayer.getCurrentState(); + videoPlayer.getCurrentPlayer().onVideoPause(); + isPause = true; + } + + @Override + protected void onResume() { + super.onResume(); + if (videoPlayState == GSYVideoView.CURRENT_STATE_PLAYING) { + videoPlayer.getCurrentPlayer().onVideoResume(false); + isPause = false; + } + } + + /** + * orientationUtils 和 detailPlayer.onConfigurationChanged 方法是用于触发屏幕旋转的 + */ + @Override + public void onConfigurationChanged(@NotNull Configuration newConfig) { + super.onConfigurationChanged(newConfig); + //如果旋转了就全屏 + if (isPlay && !isPause) { + videoPlayer.onConfigurationChanged(this, newConfig, orientationUtils, + true, true); + } + } + + @Override + protected void onDestroy() { + super.onDestroy(); + if (isPlay) { + videoPlayer.getCurrentPlayer().release(); + } + if (orientationUtils != null) + orientationUtils.releaseListener(); + } +} \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/adapter/CacheAdapter.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/adapter/CacheAdapter.java index 788d784d73..58f9af06f4 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/adapter/CacheAdapter.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/adapter/CacheAdapter.java @@ -16,6 +16,10 @@ package com.mindspore.himindspore.ui.me.adapter; import android.content.Context; +import android.content.Intent; + +import android.net.Uri; +import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -25,16 +29,23 @@ import android.widget.TextView; import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; +import com.bumptech.glide.Glide; +import com.bumptech.glide.load.resource.bitmap.RoundedCorners; +import com.bumptech.glide.request.RequestOptions; import com.mindspore.himindspore.R; +import com.mindspore.himindspore.ui.me.PlayVideoActivity; +import java.io.File; +import java.text.DecimalFormat; import java.util.ArrayList; public class CacheAdapter extends RecyclerView.Adapter { + private static final String TAG = CacheAdapter.class.getSimpleName(); private Context mContext; - private ArrayList mList = new ArrayList(); + private ArrayList mList; - public CacheAdapter(Context mContext, ArrayList mList) { + public CacheAdapter(Context mContext, ArrayList mList) { this.mContext = mContext; this.mList = mList; } @@ -47,28 +58,51 @@ public class CacheAdapter extends RecyclerView.Adapter @Override public void onBindViewHolder(@NonNull CacheAdapter.ViewHolder holder, int position) { - if (holder instanceof ViewHolder) { + String path = mList.get(position).getAbsolutePath(); + Log.d(TAG, "onBindViewHolder path:" + path); +// Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(path, MediaStore.Images.Thumbnails.MICRO_KIND); +// holder.thumbnailIv.setImageBitmap(bitmap); + Glide.with(mContext) + .load((Uri) null) + .apply(RequestOptions.bitmapTransform(new RoundedCorners( + (int) mContext.getResources().getDimension(R.dimen.listItemImageConner)))) + .error(R.drawable.icon_default) + .placeholder(R.drawable.icon_default) + .into(holder.thumbnailIv); - } + // holder.durationTv.setText(duration); + String name = mList.get(position).getName(); + String title = name.substring(0, name.lastIndexOf('.')); + holder.nameTv.setText(title); + + DecimalFormat decimalFormat = new DecimalFormat("0.00"); + float fileSize = mList.get(position).length() / (1024*1024f); // 转为M为单位 + holder.sizeTv.setText(decimalFormat.format(fileSize) + "M"); + holder.itemView.setOnClickListener(v -> { + Intent intent = new Intent(mContext, PlayVideoActivity.class); + intent.putExtra("filePath", path); + mContext.startActivity(intent); + } + ); } @Override public int getItemCount() { - return 6; + return mList.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { - ImageView imageView; - TextView name, source, size, duration; + ImageView thumbnailIv; + TextView nameTv, sourceTv, sizeTv, durationTv; public ViewHolder(@NonNull View itemView) { super(itemView); - /*imageView = itemView.findViewById(R.id.case_image); - name = itemView.findViewById(R.id.case_name); - source = itemView.findViewById(R.id.case_image); - size = itemView.findViewById(R.id.case_image); - duration = itemView.findViewById(R.id.case_image);*/ + thumbnailIv = itemView.findViewById(R.id.cache_image); + nameTv = itemView.findViewById(R.id.cache_name); + sourceTv = itemView.findViewById(R.id.cache_source); + sizeTv = itemView.findViewById(R.id.cache_size); + durationTv = itemView.findViewById(R.id.cache_duration); } } } diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/adapter/ServerInfoAdapter.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/adapter/ServerInfoAdapter.java index a26836f125..41ef404eb0 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/adapter/ServerInfoAdapter.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/adapter/ServerInfoAdapter.java @@ -35,11 +35,7 @@ public class ServerInfoAdapter extends RecyclerView.Adapter { - if (serverItemClickListener != null) { - serverItemClickListener.onServerItemClickListener(position); - } - }); + holder.itemView.setOnClickListener(infoList.get(position).getClickListener()); } @Override diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/bean/ServerInfoBean.java b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/bean/ServerInfoBean.java index e926ef516c..0dbf370458 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/bean/ServerInfoBean.java +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/java/com/mindspore/himindspore/ui/me/bean/ServerInfoBean.java @@ -1,14 +1,33 @@ package com.mindspore.himindspore.ui.me.bean; import android.graphics.drawable.Drawable; +import android.view.View; + +import com.mindspore.customview.ui.MyToast; public class ServerInfoBean { private String name; private Drawable pic; + private View.OnClickListener clickListener; + + public View.OnClickListener getClickListener() { + return clickListener; + } + + public void setClickListener(View.OnClickListener clickListener) { + this.clickListener = clickListener; + } public ServerInfoBean(String name, Drawable pic) { this.name = name; this.pic = pic; + clickListener = new View.OnClickListener() { + @Override + public void onClick(View v) { + // 默认的点击事件是 弹出“该功能未开放” + MyToast.FunctionNoOpen(); + } + }; } public String getName() { diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_lessons.xml b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_lessons.xml index 729cff1b34..7631d7a0cf 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_lessons.xml +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/activity_lessons.xml @@ -129,7 +129,7 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" - android:layout_marginStart="64dp" + android:layout_marginStart="40dp" android:orientation="vertical"> + + + + + + + + + + + + \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/adapter_layout_cache.xml b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/adapter_layout_cache.xml index 41305397fc..74cb816fa9 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/adapter_layout_cache.xml +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/layout/adapter_layout_cache.xml @@ -1,6 +1,8 @@ @@ -8,69 +10,62 @@ android:id="@+id/cache_image" android:layout_width="112dp" android:layout_height="63dp" - android:layout_marginStart="10dp" - android:layout_marginTop="11dp" + android:layout_marginTop="@dimen/spaceVerticalNormalSize" android:scaleType="centerCrop" android:src="@drawable/case_image" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> - + app:layout_constraintTop_toTopOf="@id/cache_image" /> - - + app:layout_constraintBottom_toBottomOf="@id/cache_image" /> - diff --git a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/values/strings.xml b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/values/strings.xml index 2c33d6d36b..3f34963708 100644 --- a/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/values/strings.xml +++ b/model_zoo/official/lite/MindSpore_inhand/app/src/main/res/values/strings.xml @@ -178,6 +178,9 @@ 收藏 我的课程 离线缓存 + 缓存 + 缓存中 + 已缓存 问题反馈 GITEE代码 官方网站 @@ -207,6 +210,12 @@ 历史观看课程 该功能暂未开放 没有可分享的数据 + 没有可缓存的数据 + 视频正在缓存 + 视频已经下载 + 视频缓存成功 + 视频缓存失败 + 您还没有缓存过视频 再按一次退出 倍速 diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/config/StorageUtils.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/config/StorageUtils.java index 694f9cde84..266e73d5bf 100644 --- a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/config/StorageUtils.java +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/config/StorageUtils.java @@ -6,9 +6,21 @@ import java.io.File; public class StorageUtils { - public static final String STORAGE_PHOTO = Environment.getExternalStorageDirectory() + File.separator + "photo.jpeg"; - public static final String VIDEO_PATH = Environment.getExternalStorageDirectory().getPath() + "/" + Environment.DIRECTORY_DOWNLOADS + "/"; - public static final String CRASH_PATH = Environment.getExternalStorageDirectory().getPath() + "/MindSpore/CrashLog/"; + private static final String APP_NAME = "MindSpore"; - public static final File ABSOLUTE_FILE = Environment.getExternalStorageDirectory().getAbsoluteFile(); + public static final String STORAGE_PHOTO = Environment.getExternalStorageDirectory() + + File.separator + "photo.jpeg"; + + public static final String VIDEO_PATH = Environment.getExternalStorageDirectory().getPath() + + "/" + Environment.DIRECTORY_DOWNLOADS + "/"; + + public static final String CRASH_PATH = Environment.getExternalStorageDirectory().getPath() + + "/MindSpore/CrashLog/"; + + public static final String VIDEO_DOWNLOAD_PATH = + Environment.getExternalStorageDirectory().getPath() + "/" + + Environment.DIRECTORY_DOWNLOADS + "/" + APP_NAME + "/"; + + public static final File ABSOLUTE_FILE = Environment.getExternalStorageDirectory() + .getAbsoluteFile(); } diff --git a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/http/HttpClient.java b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/http/HttpClient.java index 1b903b1fc9..7abd4159b3 100644 --- a/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/http/HttpClient.java +++ b/model_zoo/official/lite/MindSpore_inhand/common/src/main/java/com/mindspore/common/http/HttpClient.java @@ -20,6 +20,7 @@ import retrofit2.http.GET; import retrofit2.http.Headers; import retrofit2.http.POST; import retrofit2.http.Streaming; +import retrofit2.http.Url; public interface HttpClient { @@ -63,4 +64,8 @@ public interface HttpClient { @Headers("Content-Type: application/json") @POST("api/requestCourseDetail") Observable> getCourseDetailData(@Body RequestIdBean requestIdBean); + + @Streaming + @GET() + Observable downloadVideo(@Url String url); } \ No newline at end of file diff --git a/model_zoo/official/lite/MindSpore_inhand/customView/src/main/java/com/mindspore/customview/ui/MyToast.java b/model_zoo/official/lite/MindSpore_inhand/customView/src/main/java/com/mindspore/customview/ui/MyToast.java index a1e04e9433..93a645ff83 100644 --- a/model_zoo/official/lite/MindSpore_inhand/customView/src/main/java/com/mindspore/customview/ui/MyToast.java +++ b/model_zoo/official/lite/MindSpore_inhand/customView/src/main/java/com/mindspore/customview/ui/MyToast.java @@ -93,9 +93,9 @@ public class MyToast { } } - public static void MakeText(String text, int duration){ + public static void MakeText(String text, int duration) { MyToast makeView = new MyToast(Utils.getApp(), null); - makeView.show(text,duration); + makeView.show(text, duration); } public static void FunctionNoOpen(){ MyToast.MakeText(Utils.getApp().getResources().getString(R.string.function_not_open),800); diff --git a/model_zoo/official/lite/MindSpore_inhand/dance/src/main/res/values/strings.xml b/model_zoo/official/lite/MindSpore_inhand/dance/src/main/res/values/strings.xml index ea4d57e73d..309f3e5fbc 100644 --- a/model_zoo/official/lite/MindSpore_inhand/dance/src/main/res/values/strings.xml +++ b/model_zoo/official/lite/MindSpore_inhand/dance/src/main/res/values/strings.xml @@ -16,6 +16,7 @@ 抱歉,您没有授予相机访问权限 下载 下载失败 + 下载完毕 "舞蹈视频下载中 ...%1$d%2$s" 恭喜完成,综合得分: -- Gitee