<commit_before>package org.smblott.intentradio;

import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.StrictMode;

import android.app.PendingIntent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Notification.Builder;

import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnCompletionListener;

import android.net.Uri;
import android.os.Build.VERSION;

public class IntentPlayer extends Service
   implements
      OnBufferingUpdateListener,
      OnInfoListener,
      OnErrorListener,
      OnPreparedListener,
      OnAudioFocusChangeListener,
      OnCompletionListener
{

   /* ********************************************************************
    * Globals...
    */

   private static final boolean play_disabled = false;
   private static final int note_id = 100;

   private static Context context = null;
   private static PendingIntent pending = null;

   private static String app_name = null;
   private static String app_name_long = null;
   private static String intent_play = null;
   private static String intent_stop = null;
   private static String intent_pause = null;
   private static String intent_restart = null;
   private static String intent_state_request = null;
   private static String default_url = null;
   private static String default_name = null;

   public  static String name = "";
   public  static String url = "";

   private static Playlist pltask = null;
   private static MediaPlayer player = null;
   private static Builder builder = null;
   private static Notification note = null;
   private static NotificationManager note_manager = null;
   private static AudioManager audio_manager = null;

   /* ********************************************************************
    * Create service...
    */

   @Override
   public void onCreate() {
      context = getApplicationContext();
      Logger.init(context);

      app_name = getString(R.string.app_name);
      app_name_long = getString(R.string.app_name_long);
      intent_play = getString(R.string.intent_play);
      intent_stop = getString(R.string.intent_stop);
      intent_pause = getString(R.string.intent_pause);
      intent_restart = getString(R.string.intent_restart);
      intent_state_request = context.getString(R.string.intent_state_request);
      default_url = getString(R.string.default_url);
      default_name = getString(R.string.default_name);

      note_manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      pending = PendingIntent.getBroadcast(context, 0, new Intent(intent_stop), 0);

      audio_manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

      builder =
         new Notification.Builder(context)
            .setSmallIcon(R.drawable.intent_radio)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentIntent(pending)
            .setContentTitle(app_name_long)
            // not available in API 16...
            // .setShowWhen(false)
            ;
   }

   /* ********************************************************************
    * Destroy service...
    */

   public void onDestroy()
   {
      stop();

      if ( player != null )
      {
         player.release();
         player = null;
      }

      log("Destroyed.");
      Logger.state("off");
      super.onDestroy();
   }

   /* ********************************************************************
    * Primary entry point...
    */

   @Override
   public int onStartCommand(Intent intent, int flags, int startId)
   {
      if ( intent == null || ! intent.hasExtra("action") )
         return done(null);

      if ( intent.hasExtra("debug") )
         Logger.state(intent.getStringExtra("debug"));

      if ( ! Counter.still(intent.getIntExtra("counter", Counter.now())) )
         return done(null);

      String action = intent.getStringExtra("action");
      log("Action: ", action);

      if ( action.equals(intent_stop)    ) return stop();
      if ( action.equals(intent_pause)   ) return pause();
      if ( action.equals(intent_restart) ) return restart();

      if ( action.equals(intent_play) )
      {
         /* Ensure intent has a "url"...
          */
         if ( ! intent.hasExtra("url") || intent.getStringExtra("url").equals("") )
         {
            if ( url == null || url.equals("") )
            {
               intent.putExtra("url", default_url);
               intent.putExtra("name", default_name);
            }
            else
            {
               intent.putExtra("url", url);
               intent.putExtra("name", name);
            }
         }

         /* Ensure intent has a "name"...
          */
         if ( ! intent.hasExtra("name") )
            intent.putExtra("name", intent.getStringExtra("url"));

         /* Extract "url" and "name"...
          */
         url = intent.getStringExtra("url");
         name = intent.getStringExtra("name");

         log("Name: ", name);
         log("URL: ", url);
         return play(url);
      }

      if ( action.equals(intent_state_request) )
      {
         State.get_state(context);
         return done(null);
      }

      log("unknown action: ", action);
      return done(null);
   }

   /* ********************************************************************
    * Play...
    */

   public void play(String url, int then)
   {
      if ( Counter.still(then) )
         play(url);
   }

   private int play(String url)
   {
      stop(false);
      toast(name);

      int focus = audio_manager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
      if ( focus != AudioManager.AUDIOFOCUS_REQUEST_GRANTED )
         return stop("Failed to get audio focus!");

      builder.setOngoing(true).setContentText("Connecting...");
      note = builder.build();

      WifiLocker.lock(context, app_name_long);
      startForeground(note_id, note);
      log("Connecting...");

      // /////////////////////////////////////////////////////////////////
      // Set up media player...

      log("Play: ", url);

      if ( play_disabled )
         return stop();

      if ( player == null )<commit_msg>Remember state across restarts.<commit_after>package org.smblott.intentradio;

import android.app.Service;
import android.content.Intent;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.IBinder;
import android.os.PowerManager;
import android.os.StrictMode;

import android.app.PendingIntent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.Notification.Builder;

import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;

import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.media.MediaPlayer.OnCompletionListener;

import android.net.Uri;
import android.os.Build.VERSION;

public class IntentPlayer extends Service
   implements
      OnBufferingUpdateListener,
      OnInfoListener,
      OnErrorListener,
      OnPreparedListener,
      OnAudioFocusChangeListener,
      OnCompletionListener
{

   /* ********************************************************************
    * Globals...
    */

   private static final int note_id = 100;
   private static final String preference_file = "state";
   private static SharedPreferences settings = null;

   private static Context context = null;
   private static PendingIntent pending = null;

   private static String app_name = null;
   private static String app_name_long = null;
   private static String intent_play = null;
   private static String intent_stop = null;
   private static String intent_pause = null;
   private static String intent_restart = null;
   private static String intent_state_request = null;
   private static String default_url = null;
   private static String default_name = null;

   public  static String name = "";
   public  static String url = "";

   private static Playlist pltask = null;
   private static MediaPlayer player = null;
   private static Builder builder = null;
   private static Notification note = null;
   private static NotificationManager note_manager = null;
   private static AudioManager audio_manager = null;

   /* ********************************************************************
    * Create service...
    */

   @Override
   public void onCreate() {
      context = getApplicationContext();
      Logger.init(context);

      app_name = getString(R.string.app_name);
      app_name_long = getString(R.string.app_name_long);
      intent_play = getString(R.string.intent_play);
      intent_stop = getString(R.string.intent_stop);
      intent_pause = getString(R.string.intent_pause);
      intent_restart = getString(R.string.intent_restart);
      intent_state_request = context.getString(R.string.intent_state_request);
      default_url = getString(R.string.default_url);
      default_name = getString(R.string.default_name);

      settings = getSharedPreferences(preference_file, context.MODE_PRIVATE);
      url = settings.getString("url", default_url);
      name = settings.getString("name", default_name);

      note_manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
      pending = PendingIntent.getBroadcast(context, 0, new Intent(intent_stop), 0);

      audio_manager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

      builder =
         new Notification.Builder(context)
            .setSmallIcon(R.drawable.intent_radio)
            .setPriority(Notification.PRIORITY_HIGH)
            .setContentIntent(pending)
            .setContentTitle(app_name_long)
            // not available in API 16...
            // .setShowWhen(false)
            ;
   }

   /* ********************************************************************
    * Destroy service...
    */

   public void onDestroy()
   {
      stop();

      if ( player != null )
      {
         player.release();
         player = null;
      }

      log("Destroyed.");
      Logger.state("off");
      super.onDestroy();
   }

   /* ********************************************************************
    * Primary entry point...
    */

   @Override
   public int onStartCommand(Intent intent, int flags, int startId)
   {
      if ( intent == null || ! intent.hasExtra("action") )
         return done(null);

      if ( intent.hasExtra("debug") )
         Logger.state(intent.getStringExtra("debug"));

      if ( ! Counter.still(intent.getIntExtra("counter", Counter.now())) )
         return done(null);

      String action = intent.getStringExtra("action");
      log("Action: ", action);

      if ( action.equals(intent_stop)    ) return stop();
      if ( action.equals(intent_pause)   ) return pause();
      if ( action.equals(intent_restart) ) return restart();

      if ( action.equals(intent_play) )
      {
         /* Ensure intent has a "url"...
          */
         if ( ! intent.hasExtra("url") || intent.getStringExtra("url").equals("") )
         {
            if ( url == null || url.equals("") )
            {
               intent.putExtra("url", default_url);
               intent.putExtra("name", default_name);
            }
            else
            {
               intent.putExtra("url", url);
               intent.putExtra("name", name);
            }
         }

         /* Ensure intent has a "name"...
          */
         if ( ! intent.hasExtra("name") )
            intent.putExtra("name", intent.getStringExtra("url"));

         /* Extract "url" and "name"...
          */
         url = intent.getStringExtra("url");
         name = intent.getStringExtra("name");

         Editor editor = settings.edit();
         editor.putString("url", url);
         editor.putString("name", name);
         editor.commit();

         log("Name: ", name);
         log("URL: ", url);
         return play(url);
      }

      if ( action.equals(intent_state_request) )
      {
         State.get_state(context);
         return done(null);
      }

      log("unknown action: ", action);
      return done(null);
   }

   /* ********************************************************************
    * Play...
    */

   public void play(String url, int then)
   {
      if ( Counter.still(then) )
         play(url);
   }

   private int play(String url)
   {
      stop(false);
      toast(name);

      int focus = audio_manager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
      if ( focus != AudioManager.AUDIOFOCUS_REQUEST_GRANTED )
         return stop("Failed to get audio focus!");

      builder.setOngoing(true).setContentText("Connecting...");
      note = builder.build();

      WifiLocker.lock(context, app_name_long);
      startForeground(note_id, note);
      log("Connecting...");

      // /////////////////////////////////////////////////////////////////
      // Set up media player...

      log("Play: ", url);

      if ( player == null )
{'fraction_non_alphanumeric': 0.16422930681485814, 'fraction_numerical': 0.0010236911377595789, 'mean_word_length': 2.70148849797023, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '816282', 'n_tokens_mistral': 3681, 'n_tokens_neox': 3545, 'n_words': 779}