[android] custom notification bar

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:gravity="center_vertical"
    android:orientation="horizontal"
    android:paddingBottom="10dp"
    android:paddingTop="10dp" >

    <LinearLayout
        android:id="@+id/layout_title"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="horizontal" >

        <ImageView
            android:id="@android:id/icon"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:adjustViewBounds="true"
            android:src="@drawable/ic_launcher" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <TextView
                android:id="@android:id/content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="@string/hello_world"
                android:textAppearance="?android:attr/textAppearanceSmall" />
        </LinearLayout>
    </LinearLayout>

    <ImageButton
        android:id="@+id/btn_rew"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@android:drawable/ic_media_rew" />

    <ImageButton
        android:id="@+id/btn_toggle_play"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@android:drawable/ic_media_play" />

    <ImageButton
        android:id="@+id/btn_ff"
        style="?android:attr/buttonBarButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@android:drawable/ic_media_ff" />

</LinearLayout>

 

 

<resources>

    <string name="app_name">RemoteViewSample</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_play">kr.mint.remoteviewsample.action_play</string>
    <string name="action_pause">kr.mint.remoteviewsample.action_pause</string>
    <string name="action_rew">kr.mint.remoteviewsample.action_rew</string>
    <string name="action_ff">kr.mint.remoteviewsample.action_ff</string>

</resources>

 

public class MainActivity extends Activity
{
  private NotificationHelper _notificationHelper;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    _notificationHelper = new NotificationHelper(getApplicationContext());
    
    IntentFilter filter = new IntentFilter();
    filter.addAction(getString(R.string.action_rew));
    filter.addAction(getString(R.string.action_ff));
    filter.addAction(getString(R.string.action_play));
    filter.addAction(getString(R.string.action_pause));
    registerReceiver(broadcastReceiver, filter);
  }
  
  
  @Override
  protected void onDestroy()
  {
    super.onDestroy();
    unregisterReceiver(broadcastReceiver);
  }
  
  private BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
  {
    @Override
    public void onReceive(Context $context, Intent $intent)
    {
      String action = $intent.getAction();
      String[] split = action.split("\\.");
      _notificationHelper.putStatusDescription(split[split.length - 1]);
      
      if (action.contains("play"))
        _notificationHelper.showPauseButton();
      else if (action.contains("pause"))
        _notificationHelper.showPlayButton();
    }
  };
}

 

public class NotificationHelper
{
  private static final int NOTIFICATION_ID = 123456789;
  
  private RemoteViews _smallView;
  private Notification _notification;
  private Intent _playToggleIntent;
  private PendingIntent _playTogglePendingIntent;
  private Context _context;
  private NotificationCompat.Builder _notificationBuilder;
  private NotificationManager _notificationManager;
  
  
  public NotificationHelper(Context $context)
  {
    _context = $context;
    
    Intent rewIntent = new Intent(_context.getString(R.string.action_rew));
    PendingIntent rewPendingIntent = PendingIntent.getBroadcast(_context, 1, rewIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    Intent ffIntent = new Intent(_context.getString(R.string.action_ff));
    PendingIntent ffPendingIntent = PendingIntent.getBroadcast(_context, 2, ffIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    _playToggleIntent = new Intent(_context.getString(R.string.action_play));
    _playTogglePendingIntent = PendingIntent.getBroadcast(_context, 3, _playToggleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    Intent runIntent = new Intent(_context, MainActivity.class);
    PendingIntent runPendingIntent = PendingIntent.getActivity(_context, 4, runIntent, PendingIntent.FLAG_CANCEL_CURRENT);
    
    _smallView = new RemoteViews(_context.getPackageName(), R.layout.view_notification);
    _smallView.setOnClickPendingIntent(R.id.btn_rew, rewPendingIntent);
    _smallView.setOnClickPendingIntent(R.id.btn_ff, ffPendingIntent);
    _smallView.setOnClickPendingIntent(R.id.btn_toggle_play, _playTogglePendingIntent);
    _smallView.setOnClickPendingIntent(R.id.layout_title, runPendingIntent);
    
    Resources res = _context.getResources();
    _notificationBuilder = new NotificationCompat.Builder(_context).setSmallIcon(R.drawable.ic_launcher).setTicker(res.getString(R.string.app_name)).setContentTitle(res.getString(R.string.app_name));
    _notification = _notificationBuilder.build();
    _notification.flags |= Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
    _notificationManager = (NotificationManager) _context.getSystemService(Context.NOTIFICATION_SERVICE);
    
    buildAndShow();
  }
  
  
  public void putStatusDescription(String $str)
  {
    _smallView.setTextViewText(android.R.id.content, $str);
    
    buildAndShow();
  }
  
  
  public void showPlayButton()
  {
    _playToggleIntent.setAction(_context.getString(R.string.action_play));
    _playTogglePendingIntent = PendingIntent.getBroadcast(_context, 3, _playToggleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    _smallView.setImageViewResource(R.id.btn_toggle_play, android.R.drawable.ic_media_play);
    _smallView.setOnClickPendingIntent(R.id.btn_toggle_play, _playTogglePendingIntent);
    
    buildAndShow();
  }
  
  
  public void showPauseButton()
  {
    _smallView.setImageViewResource(R.id.btn_toggle_play, android.R.drawable.ic_media_pause);
    
    _playToggleIntent.setAction(_context.getString(R.string.action_pause));
    _playTogglePendingIntent = PendingIntent.getBroadcast(_context, 3, _playToggleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    _smallView.setOnClickPendingIntent(R.id.btn_toggle_play, _playTogglePendingIntent);
    
    buildAndShow();
  }
  
  
  private void buildAndShow()
  {
    _notification.contentView = _smallView;
    
    _notificationManager.notify(NOTIFICATION_ID, _notification);
  }
  
  
  public void hide()
  {
    _notificationManager.cancelAll();
  }
}