Android Open Source - PlayTunes Check Remote Version File Task






From Project

Back to project page PlayTunes.

License

The source code is released under:

Copyright (c) 2014, Impeccable Labs, LLC. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions ar...

If you think the Android project PlayTunes 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

package com.ideabag.playtunes.util;
/*w w w  .  j  a v a  2s . co m*/
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.Scanner;

import org.json.JSONException;
import org.json.JSONObject;

import com.ideabag.playtunes.R;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Handler;
import android.view.View;

public class CheckRemoteVersionFileTask extends AsyncTask< String, Void, JSONObject> {
  
  private static final String VERSION_FILE_URL = "http://version.playtunesapp.com";
  private static final String UPDATE_PREF_FILE = "update_pref_file";
  private static final String UPDATE_PREF_KEY = "update_timestamp";
  
  private static final int ONE_WEEK_MILLI = 604800000;
  
  Context mContext;
  SharedPreferences prefs;
  
  int mVersionCode = 0;
  
  public CheckRemoteVersionFileTask( Context context) {
    
    mContext = context;
    prefs = mContext.getSharedPreferences( UPDATE_PREF_FILE, Context.MODE_PRIVATE );
    
    try {
      mVersionCode = mContext.getPackageManager().getPackageInfo( mContext.getPackageName(), 0).versionCode;
    } catch (NameNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    
  }
  
/*
  This is what the JSON file looks like:
  
  {
    
    versionInfo: {
      
      code: 9,
      name: "4.0",
      
    },
    
    changelog: "",
    
  }
*/
  
  private static final String VERSION_INFO = "versionInfo";
  private static final String VERSION_CODE = "versionCode";
  private static final String VERSION_NAME = "versionName";
  private static final String CHANGE_LOG = "changelog";
  

  
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected JSONObject doInBackground( String... urls ) {
      
      JSONObject job = null;
      String result = "";
      
      long then = prefs.getLong( UPDATE_PREF_KEY, 0 );
      long mNow = new Date().getTime();
      
      
      if ( mNow - ONE_WEEK_MILLI < then ) {
        
        cancel( true );
        
      } else {
        
        try {
          //HttpURLConnection.setFollowRedirects( true );
          URL url = new URL( VERSION_FILE_URL );
          HttpURLConnection mUrlConnection = ( HttpURLConnection ) url.openConnection();
          mUrlConnection.setUseCaches( false );
          //mUrlConnection.setInstanceFollowRedirects( true );
          
          while ( true ) {
            switch ( mUrlConnection.getResponseCode() ) {
              
                  case HttpURLConnection.HTTP_MOVED_PERM:
                  case HttpURLConnection.HTTP_MOVED_TEMP:
                    
                    url = new URL( mUrlConnection.getHeaderField( "Location" ) );
                    mUrlConnection.disconnect();
                    mUrlConnection = ( HttpURLConnection ) url.openConnection();
                     continue;
                     
               }
            
            break;
            
          }
          
          long lastModified = mUrlConnection.getLastModified();
          
          if ( mNow > lastModified ) {
            
            //mUrlConnection.connect();
            
          InputStream in = new BufferedInputStream( mUrlConnection.getInputStream() );
              
          @SuppressWarnings("resource")
          Scanner s = new Scanner( in ).useDelimiter("\\A");
            
          while ( s.hasNext() ) {
            
            result = result.concat( s.next() );
            
          }
          
          s.close();
          
          //mUrlConnection.disconnect();
          
          job = new JSONObject( result );
          
          int remoteVersionCode = job.getInt( VERSION_CODE );
          
          SharedPreferences.Editor edit = prefs.edit();
          
          if ( mVersionCode < remoteVersionCode ) {
            
            edit.putLong( UPDATE_PREF_KEY, 0 );
            
          } else {
            
            edit.putLong( UPDATE_PREF_KEY, mNow );
            
          }
          
          edit.commit();
          
          }
        
      } catch ( IOException e ) {
          
        e.printStackTrace();
          
      } catch ( JSONException e ) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        android.util.Log.i( "CheckRemoveVersionFile", "" + result );
      }
        
      }
      
        return job;
    }
    
    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute( JSONObject result ) {
        
      if ( !isCancelled() ) {
      
        if ( null != result ) {
          
          try {
            
            int remoteVersionCode = result.getInt( VERSION_CODE );
            String changelog = result.getString( CHANGE_LOG );
            
            int versionCode = mContext.getPackageManager()
                  .getPackageInfo( mContext.getPackageName(), 0).versionCode;
            
            if ( versionCode < remoteVersionCode ) {
              
              //handle.post( notUpToDate );
              
              new AlertDialog.Builder( mContext )
                          .setTitle( mContext.getString( R.string.update_app ) )
                          .setMessage( mContext.getString( R.string.version_outdated ) + ( changelog != null && changelog.length() > 1 ? "\n\n" + changelog : "" ) )
                          .setNegativeButton( mContext.getString( R.string.cancel ), mClickListener)
                          .setPositiveButton( mContext.getString( R.string.update ), mClickListener)
                          .show();
              
              
            }
            
            
          } catch( Exception e ) {
            
            
            
          }
          
        }
        
      }
      
    }
    
    DialogInterface.OnClickListener mClickListener = new DialogInterface.OnClickListener() {
    
    @Override public void onClick( DialogInterface arg0, int arg1 ) {
      
      if ( arg1 == DialogInterface.BUTTON_POSITIVE ) {
        
        final String appPackageName = mContext.getPackageName(); // getPackageName() from Context or Activity object
        
        try {
          
            mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
        
        } catch (android.content.ActivityNotFoundException anfe) {
          
          mContext.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + appPackageName)));
          
        }
        
      }
      
    }
    
  };
  
}




Java Source Code List

com.ideabag.playtunes.PlaylistManager.java
com.ideabag.playtunes.DragNDrop.DragListener.java
com.ideabag.playtunes.DragNDrop.DragNDropListView.java
com.ideabag.playtunes.DragNDrop.DropListener.java
com.ideabag.playtunes.DragNDrop.RemoveListener.java
com.ideabag.playtunes.activity.MainActivity.java
com.ideabag.playtunes.activity.NowPlayingActivity.java
com.ideabag.playtunes.activity.SettingsActivity.java
com.ideabag.playtunes.adapter.AlbumListAdapter.java
com.ideabag.playtunes.adapter.AlbumsAllAdapter.java
com.ideabag.playtunes.adapter.AlbumsOneAdapter.java
com.ideabag.playtunes.adapter.ArtistAlbumsAdapter.java
com.ideabag.playtunes.adapter.ArtistAllSongsAdapter.java
com.ideabag.playtunes.adapter.ArtistListAdapter.java
com.ideabag.playtunes.adapter.ArtistSinglesAdapter.java
com.ideabag.playtunes.adapter.ArtistsAllAdapter.java
com.ideabag.playtunes.adapter.AsyncQueryAdapter.java
com.ideabag.playtunes.adapter.GenresAllAdapter.java
com.ideabag.playtunes.adapter.GenresOneAdapter.java
com.ideabag.playtunes.adapter.NavigationListAdapter.java
com.ideabag.playtunes.adapter.PlaylistsAllAdapter.java
com.ideabag.playtunes.adapter.PlaylistsOneAdapter.java
com.ideabag.playtunes.adapter.SongListAdapter.java
com.ideabag.playtunes.adapter.SongsAllAdapter.java
com.ideabag.playtunes.adapter.search.SearchAlbumsAdapter.java
com.ideabag.playtunes.adapter.search.SearchArtistsAdapter.java
com.ideabag.playtunes.adapter.search.SearchSongsAdapter.java
com.ideabag.playtunes.database.MediaQuery.java
com.ideabag.playtunes.dialog.AddToPlaylistDialogFragment.java
com.ideabag.playtunes.dialog.CreatePlaylistDialogFragment.java
com.ideabag.playtunes.dialog.FeedbackDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistDeleteDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistMenuDialogFragment.java
com.ideabag.playtunes.dialog.PlaylistRenameDialogFragment.java
com.ideabag.playtunes.dialog.RateAppDialogFragment.java
com.ideabag.playtunes.dialog.SongMenuDialogFragment.java
com.ideabag.playtunes.fragment.AlbumsAllFragment.java
com.ideabag.playtunes.fragment.AlbumsOneFragment.java
com.ideabag.playtunes.fragment.ArtistSinglesFragment.java
com.ideabag.playtunes.fragment.ArtistsAllFragment.java
com.ideabag.playtunes.fragment.ArtistsOneFragment.java
com.ideabag.playtunes.fragment.FooterControlsFragment.java
com.ideabag.playtunes.fragment.GenresAllFragment.java
com.ideabag.playtunes.fragment.GenresOneFragment.java
com.ideabag.playtunes.fragment.MusicBrowserFragment.java
com.ideabag.playtunes.fragment.NavigationDrawerFragment.java
com.ideabag.playtunes.fragment.NavigationFragment.java
com.ideabag.playtunes.fragment.PlaylistsAllFragment.java
com.ideabag.playtunes.fragment.PlaylistsOneFragment.java
com.ideabag.playtunes.fragment.SaveScrollListFragment.java
com.ideabag.playtunes.fragment.SongsFragment.java
com.ideabag.playtunes.fragment.TrackProgressFragment.java
com.ideabag.playtunes.fragment.search.SearchAlbumsFragment.java
com.ideabag.playtunes.fragment.search.SearchAllFragment.java
com.ideabag.playtunes.fragment.search.SearchArtistsFragment.java
com.ideabag.playtunes.fragment.search.SearchFragment.java
com.ideabag.playtunes.fragment.search.SearchSongsFragment.java
com.ideabag.playtunes.fragment.search.SearchSuggestionsFragment.java
com.ideabag.playtunes.media.AudioFocusHelper.java
com.ideabag.playtunes.media.MusicFocusable.java
com.ideabag.playtunes.media.MusicIntentReceiver.java
com.ideabag.playtunes.media.PlaylistMediaPlayer.java
com.ideabag.playtunes.media.RemoteControlClientCompat.java
com.ideabag.playtunes.media.RemoteControlHelper.java
com.ideabag.playtunes.service.LockscreenManager.java
com.ideabag.playtunes.service.MusicPlayerService.java
com.ideabag.playtunes.service.PlaybackNotification.java
com.ideabag.playtunes.util.AdmobUtil.java
com.ideabag.playtunes.util.AlbumSongsCountTask.java
com.ideabag.playtunes.util.AsyncDrawable.java
com.ideabag.playtunes.util.BitmapWorkerTask.java
com.ideabag.playtunes.util.CheckRemoteVersionFileTask.java
com.ideabag.playtunes.util.DrawerLayout.java
com.ideabag.playtunes.util.GAEvent.java
com.ideabag.playtunes.util.IMusicBrowser.java
com.ideabag.playtunes.util.ISearchableAdapter.java
com.ideabag.playtunes.util.LoadAlbumStackTask.java
com.ideabag.playtunes.util.MergeAdapter.java
com.ideabag.playtunes.util.QueryCountTask.java
com.ideabag.playtunes.util.SackOfViewsAdapter.java
com.ideabag.playtunes.util.SearchHistory.java
com.ideabag.playtunes.util.StarToggleTask.java
com.ideabag.playtunes.util.TrackerSingleton.java