[android] custom big notification bar

custom notification bar에서 이어짐

 

 

screenshot

screenshot 1

처음부터 big view로 시작할 수도 있는데, 아마 이건 오류로 보인다.

다른 푸시가 오면 다시 작아지더라

 

 

<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" >

    <ImageView
        android:id="@android:id/icon"
        android:layout_width="100dp"
        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" >

        <LinearLayout
            android:id="@+id/layout_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            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
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <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>
    </LinearLayout>

</LinearLayout>

 

public class NotificationHelper
{
  private static final int NOTIFICATION_ID = 123456789;
  
  private RemoteViews _smallView, _bigView;
  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);
    
    _bigView = new RemoteViews(_context.getPackageName(), R.layout.view_notification_big);
    _bigView.setOnClickPendingIntent(R.id.btn_rew, rewPendingIntent);
    _bigView.setOnClickPendingIntent(R.id.btn_ff, ffPendingIntent);
    _bigView.setOnClickPendingIntent(R.id.btn_toggle_play, _playTogglePendingIntent);
    _bigView.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);
    _bigView.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);
    
    _bigView.setImageViewResource(R.id.btn_toggle_play, android.R.drawable.ic_media_play);
    _bigView.setOnClickPendingIntent(R.id.btn_toggle_play, _playTogglePendingIntent);
    
    buildAndShow();
  }
  
  
  public void showPauseButton()
  {
    _playToggleIntent.setAction(_context.getString(R.string.action_pause));
    _playTogglePendingIntent = PendingIntent.getBroadcast(_context, 3, _playToggleIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    
    _smallView.setImageViewResource(R.id.btn_toggle_play, android.R.drawable.ic_media_pause);
    _smallView.setOnClickPendingIntent(R.id.btn_toggle_play, _playTogglePendingIntent);
    
    _bigView.setImageViewResource(R.id.btn_toggle_play, android.R.drawable.ic_media_pause);
    _bigView.setOnClickPendingIntent(R.id.btn_toggle_play, _playTogglePendingIntent);
    
    buildAndShow();
  }
  
  
  private void buildAndShow()
  {
    _notification.contentView = _smallView;
    _notification.bigContentView = _bigView;
     
    _notificationManager.notify(NOTIFICATION_ID, _notification);
  }
  
  
  public void hide()
  {
    _notificationManager.cancelAll();
  }
}