Guest User

TrackInfoUpdator

a guest
Jul 22nd, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. public class TrackInfoUpdator {
  2.  
  3.     private static final String DEFAULT_TILE = "default_title";
  4.     private static final String DEFAULT_ARTIST = "default_artist";
  5.  
  6.     public static void demo() {
  7.         Callback callback = (title, artist) -> {
  8.             titleTextView.setText(title);
  9.             artistTextView.setText(artist);
  10.         };
  11.  
  12.         // this is field of your activity/fragment
  13.         TrackInfoUpdator updator = new TrackInfoUpdator(callback);
  14.  
  15.         // somewhere in onResume()
  16.         updator.startTracking("anyLink here");
  17.  
  18.         // somewhere in onPause()
  19.         updator.stopTracking();
  20.     }
  21.  
  22.  
  23.     private final Handler uiHandler;
  24.     private final Callback callback;
  25.  
  26.     // not marked as volatile, used only from UI thread
  27.     private UpdatorThread backgroundThread;
  28.  
  29.     public TrackInfoUpdator(Callback callback) {
  30.         this.uiHandler = new Handler(Looper.getMainLooper());
  31.         this.callback = callback;
  32.     }
  33.  
  34.     @UiThread
  35.     public void startTracking(String link) {
  36.         stopTracking();
  37.         backgroundThread = new UpdatorThread(uiHandler, callback, link);
  38.         backgroundThread.start();
  39.     }
  40.  
  41.     @UiThread
  42.     public void stopTracking() {
  43.         if (backgroundThread != null) {
  44.             backgroundThread.disable();
  45.             backgroundThread = null;
  46.         }
  47.     }
  48.    
  49.     private static class UpdatorThread extends Thread {
  50.  
  51.         private static final int MSECS_REPEAT = 3000;
  52.  
  53.         private final Handler callbackThread;
  54.         private final Callback callback;
  55.         private final String link;
  56.         private volatile boolean isDisabled = false;
  57.  
  58.         public UpdatorThread(Handler callbackThread, Callback callback, String link) {
  59.             this.callbackThread = callbackThread;
  60.             this.callback = callback;
  61.             this.link = link;
  62.         }
  63.  
  64.         public void disable() {
  65.             isDisabled = true;
  66.         }
  67.  
  68.         @Override
  69.         public void run() {
  70.             while (!isDisabled) {
  71.                 try {
  72.                     waitForRepeat();
  73.                 } catch (Exception ignored) {
  74.                 }
  75.                 try {
  76.                     fetchData();
  77.                 } catch (Exception ignored) {
  78.                 }
  79.             }
  80.         }
  81.  
  82.         private void waitForRepeat() throws Exception {
  83.             Thread.sleep(MSECS_REPEAT);
  84.         }
  85.  
  86.         private void fetchData() throws Exception {
  87.             URL url = new URL(link);
  88.             ParsingHeaderData streaming = new ParsingHeaderData();
  89.             Utils.TrackData trackData = streaming.getTrackDetails(url);
  90.  
  91.             String title = TextUtils.isEmpty(trackData.title) ? DEFAULT_TITLE : trackData.title;
  92.             String artist = TextUtils.isEmpty(trackData.artist) ? DEFAULT_ARTIST : trackData.artist;
  93.  
  94.             callbackThread.post(() -> {
  95.                 if (!isDisabled) {
  96.                     callback.onTrackInfoUpdated(title, artist);
  97.                 }
  98.             });
  99.         }
  100.     }
  101.  
  102.     public interface Callback {
  103.         void onTrackInfoUpdated(String title, String artist);
  104.     }
  105.  
  106. }
Add Comment
Please, Sign In to add comment