Android Open Source - VideoInfoViewer Video Cache






From Project

Back to project page VideoInfoViewer.

License

The source code is released under:

Copyright (c) 2014 Rory Hool Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. Unless required by applicab...

If you think the Android project VideoInfoViewer listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
   Copyright (c) 2014 Rory Hool//from  w  ww  .j  av  a2s  .  c  om
   
   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.roryhool.videoinfoviewer.utils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.roryhool.videoinfoviewer.data.Video;

public class VideoCache {

   private String RECENT_VIDEO_PREFS = "RECENT_VIDEO_PREFS";
   private String RECENT_VIDEO_KEY = "RECENT_VIDEO_KEY";

   private int CurrentCacheId = 0;

   private int MAX_VIDEOS = 5;

   private static VideoCache mInstance = null;

   public static VideoCache Instance( Context context ) {
      if ( mInstance == null ) {
         mInstance = new VideoCache( context );
      }
      return mInstance;
   }

   private Context mContext;

   ArrayList<Video> mVideoList = new ArrayList<Video>();

   public VideoCache( Context context ) {
      mContext = context;
      initiateArray();
   }

   @SuppressWarnings( "unchecked" )
   public List<Video> getVideos() {
      ArrayList<Video> clone = (ArrayList<Video>) mVideoList.clone();
      return clone.subList( 0, Math.min( MAX_VIDEOS, mVideoList.size() ) );
   }

   public void addVideo( Video video ) {
      for ( Video existingVideo : mVideoList ) {
         if ( video.compareTo( existingVideo ) == 0 ) {
            mVideoList.remove( existingVideo );
            break;
         }
      }
      if ( video.CacheId == -1 ) {
         video.CacheId = CurrentCacheId;
         CurrentCacheId += 1;
      }
      mVideoList.add( 0, video );
      saveVideos();
   }

   public Video getVideoById( int id ) {
      for ( Video video : mVideoList ) {
         if ( video.CacheId == id ) {
            return video;
         }
      }

      return null;
   }

   @SuppressWarnings( "unchecked" )
   private void initiateArray() {

      SharedPreferences settings = mContext.getSharedPreferences( RECENT_VIDEO_PREFS, Context.MODE_PRIVATE );
      String json = settings.getString( RECENT_VIDEO_KEY, null );

      if ( json == null ) {
         return;
      }

      Type listOfVideoObject = new TypeToken<ArrayList<Video>>() {}.getType();
      
      Gson gson = new Gson();
      mVideoList = (ArrayList<Video>) gson.fromJson( json, listOfVideoObject );

      for ( Video video : mVideoList ) {
         video.CacheId = CurrentCacheId;
         CurrentCacheId += 1;
      }

      Log.d( "Test", "From json " + json + " got list of length " + mVideoList.size() );
   }

   private void saveVideos() {

      Gson gson = new Gson();
      
      String json = gson.toJson( mVideoList );
      SharedPreferences settings = mContext.getSharedPreferences( RECENT_VIDEO_PREFS, Context.MODE_PRIVATE );
      SharedPreferences.Editor editor = settings.edit();
      editor.putString( RECENT_VIDEO_KEY, json );
      editor.apply();
   }
}




Java Source Code List

com.roryhool.videoinfoviewer.AtomActivity.java
com.roryhool.videoinfoviewer.BuildConfig.java
com.roryhool.videoinfoviewer.CreditsFragment.java
com.roryhool.videoinfoviewer.Extras.java
com.roryhool.videoinfoviewer.MainActivity.java
com.roryhool.videoinfoviewer.SearchActivity.java
com.roryhool.videoinfoviewer.SearchFragment.java
com.roryhool.videoinfoviewer.VideoActivity.java
com.roryhool.videoinfoviewer.VideoInfoViewerApp.java
com.roryhool.videoinfoviewer.analytics.Analytics.java
com.roryhool.videoinfoviewer.animation.ResizeAnimation.java
com.roryhool.videoinfoviewer.atomfragments.AtomInfoFragment.java
com.roryhool.videoinfoviewer.atomfragments.AtomStructureFragment.java
com.roryhool.videoinfoviewer.data.Video.java
com.roryhool.videoinfoviewer.search.SearchItem.java
com.roryhool.videoinfoviewer.utils.AtomHelper.java
com.roryhool.videoinfoviewer.utils.BoxUtils.java
com.roryhool.videoinfoviewer.utils.CursorHelper.java
com.roryhool.videoinfoviewer.utils.FontManager.java
com.roryhool.videoinfoviewer.utils.FormatUtils.java
com.roryhool.videoinfoviewer.utils.IsoFileCache.java
com.roryhool.videoinfoviewer.utils.Logg.java
com.roryhool.videoinfoviewer.utils.UriHelper.java
com.roryhool.videoinfoviewer.utils.VideoCache.java
com.roryhool.videoinfoviewer.utils.ViewUtils.java
com.roryhool.videoinfoviewer.views.BoxInfoView.java
com.roryhool.videoinfoviewer.views.BoxView.java
com.roryhool.videoinfoviewer.views.DisableableScrollView.java
com.roryhool.videoinfoviewer.views.RobotoTextView.java
com.roryhool.videoinfoviewer.views.ScaledTextureView.java
com.roryhool.videoinfoviewer.views.VideoPlayerView.java