new notification compat for persisnent notification

This commit is contained in:
Alexey Kuznetsov 2018-10-25 13:42:46 +03:00
commit 0625baf5ec
7 changed files with 169 additions and 35 deletions

View file

@ -57,5 +57,5 @@ android {
dependencies {
testImplementation 'junit:junit:4.12'
implementation 'com.github.axet:android-audio-library:1.0.127' // implementation project(':android-audio-library')
implementation 'com.github.axet:android-audio-library:1.0.129' // implementation project(':android-audio-library')
}

View file

@ -35,6 +35,7 @@
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
@ -75,7 +76,6 @@
android:name="com.github.axet.androidlibrary.services.StorageProvider"
android:authorities="com.github.axet.audiorecorder"
android:exported="false"
android:grantUriPermissions="true"></provider>
android:grantUriPermissions="true" />
</application>
</manifest>
</manifest>

View file

@ -131,9 +131,8 @@ public class MainActivity extends AppCompatThemeActivity {
getMenuInflater().inflate(R.menu.menu_main, menu);
KeyguardManager myKM = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if (myKM.inKeyguardRestrictedInputMode()) {
if (myKM.inKeyguardRestrictedInputMode())
menu.findItem(R.id.action_settings).setVisible(false);
}
MenuItem item = menu.findItem(R.id.action_show_folder);
Intent intent = StorageProvider.getProvider().openFolderIntent(storage.getStoragePath());
@ -202,11 +201,6 @@ public class MainActivity extends AppCompatThemeActivity {
final SharedPreferences shared = PreferenceManager.getDefaultSharedPreferences(this);
if (shared.getBoolean(AudioApplication.PREFERENCE_CONTROLS, false))
showLocked(getWindow());
else
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
invalidateOptionsMenu(); // update storage folder intent
try {

View file

@ -119,10 +119,10 @@ public class RecordingActivity extends AppCompatThemeActivity {
MediaSessionCompat msc;
public static void startActivity(Context context, boolean pause) {
Log.d(TAG, "startActivity");
Intent i = new Intent(context, RecordingActivity.class);
if (pause) {
if (pause)
i.setAction(RecordingActivity.START_PAUSE);
}
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(i);
@ -277,6 +277,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "onCreate");
showLocked(getWindow());
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
@ -540,7 +541,12 @@ public class RecordingActivity extends AppCompatThemeActivity {
void stopRecording() {
if (thread != null) {
interrupt.set(true);
interrupt.set(true); // soft interrupt
try {
thread.join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
thread = null;
}
pitch.stop();
@ -854,7 +860,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
interrupt = new AtomicBoolean(false);
thread = new Thread(new Runnable() {
thread = new Thread("RecordingThread") {
@Override
public void run() {
if (old != null) {
@ -977,7 +983,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
}
}
}
}, "RecordingThread");
};
thread.start();
RecordingService.startService(this, Storage.getDocumentName(targetUri), thread != null, encoder != null, duration);
@ -1104,9 +1110,7 @@ public class RecordingActivity extends AppCompatThemeActivity {
@Override
public void run() { // success
Storage.delete(encoder.in); // delete raw recording
last.run();
d.cancel();
}
}, new Runnable() {

View file

@ -8,6 +8,7 @@ import android.content.SharedPreferences;
import android.os.Build;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import com.github.axet.androidlibrary.widgets.NotificationChannelCompat;
@ -38,6 +39,7 @@ public class AudioApplication extends com.github.axet.audiolibrary.app.MainAppli
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "onCreate");
channelStatus = new NotificationChannelCompat(this, "status", "Status", NotificationManagerCompat.IMPORTANCE_LOW);

View file

@ -18,7 +18,6 @@ import android.view.View;
import com.github.axet.androidlibrary.widgets.ProximityShader;
import com.github.axet.androidlibrary.widgets.RemoteNotificationCompat;
import com.github.axet.androidlibrary.widgets.RemoteViewsCompat;
import com.github.axet.audiolibrary.app.Storage;
import com.github.axet.audiorecorder.R;
import com.github.axet.audiorecorder.activities.MainActivity;
@ -28,12 +27,7 @@ import com.github.axet.audiorecorder.app.AudioApplication;
import java.io.File;
/**
* RecordingActivity more likly to be removed from memory when paused then service. Notification button
* does not handle getActvity without unlocking screen. The only option is to have Service.
* <p/>
* So, lets have it.
* <p/>
* Maybe later this class will be converted for fully feature recording service with recording thread.
* Sometimes RecordingActivity started twice when launched from lockscreen. We need service and move recording into Application object.
*/
public class RecordingService extends Service {
public static final String TAG = RecordingService.class.getSimpleName();
@ -113,7 +107,7 @@ public class RecordingService extends Service {
storage = new Storage(this);
showNotificationAlarm(true, new Intent());
showNotification(true, new Intent());
}
@Override
@ -123,7 +117,7 @@ public class RecordingService extends Service {
if (intent != null) {
String a = intent.getAction();
if (a == null) {
showNotificationAlarm(true, intent);
showNotification(true, intent);
} else if (a.equals(PAUSE_BUTTON)) {
Intent i = new Intent(RecordingActivity.PAUSE_BUTTON);
sendBroadcast(i);
@ -157,7 +151,7 @@ public class RecordingService extends Service {
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestory");
showNotificationAlarm(false, null);
showNotification(false, null);
}
@SuppressLint("RestrictedApi")
@ -167,10 +161,6 @@ public class RecordingService extends Service {
boolean encoding = intent.getBooleanExtra("encoding", false);
String duration = intent.getStringExtra("duration");
PendingIntent main = PendingIntent.getService(this, 0,
new Intent(this, RecordingService.class).setAction(SHOW_ACTIVITY).putExtra("targetFile", targetFile).putExtra("recording", recording),
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pe = PendingIntent.getService(this, 0,
new Intent(this, RecordingService.class).setAction(PAUSE_BUTTON),
PendingIntent.FLAG_UPDATE_CURRENT);
@ -179,11 +169,12 @@ public class RecordingService extends Service {
new Intent(this, RecordingService.class).setAction(RECORD_BUTTON),
PendingIntent.FLAG_UPDATE_CURRENT);
RemoteNotificationCompat.Builder builder = new RemoteNotificationCompat.Builder(this, R.layout.notifictaion);
RemoteNotificationCompat.Builder builder;
String title;
String text;
if (targetFile == null) {
builder = new RemoteNotificationCompat.Low(this, R.layout.notifictaion);
title = getString(R.string.app_name);
Uri f = storage.getStoragePath();
long free = storage.getFree(f);
@ -191,7 +182,10 @@ public class RecordingService extends Service {
text = AudioApplication.formatFree(this, free, sec);
builder.setViewVisibility(R.id.notification_record, View.VISIBLE);
builder.setViewVisibility(R.id.notification_pause, View.GONE);
PendingIntent main = PendingIntent.getActivity(this, 0, new Intent(this, RecordingService.class), PendingIntent.FLAG_UPDATE_CURRENT);
builder.setContentIntent(main);
} else {
builder = new RemoteNotificationCompat.Builder(this, R.layout.notifictaion);
if (recording)
title = getString(R.string.recording_title);
else
@ -201,6 +195,12 @@ public class RecordingService extends Service {
text = ".../" + targetFile;
builder.setViewVisibility(R.id.notification_record, View.GONE);
builder.setViewVisibility(R.id.notification_pause, View.VISIBLE);
PendingIntent main = PendingIntent.getService(this, 0,
new Intent(this, RecordingService.class).setAction(SHOW_ACTIVITY)
.putExtra("targetFile", targetFile)
.putExtra("recording", recording),
PendingIntent.FLAG_UPDATE_CURRENT);
builder.setMainIntent(main);
}
if (encoding) {
@ -218,7 +218,6 @@ public class RecordingService extends Service {
.setImageViewTint(R.id.icon_circle, R.attr.colorButtonNormal)
.setTitle(title)
.setText(text)
.setMainIntent(main)
.setWhen(notification)
.setOngoing(true)
.setSmallIcon(R.drawable.ic_mic);
@ -226,7 +225,7 @@ public class RecordingService extends Service {
return builder.build();
}
public void showNotificationAlarm(boolean show, Intent intent) {
public void showNotification(boolean show, Intent intent) {
NotificationManagerCompat nm = NotificationManagerCompat.from(this);
if (!show) {
stopForeground(false);

View file

@ -0,0 +1,135 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/notification_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clipChildren="false"
android:gravity="center"
android:orientation="horizontal"
android:paddingLeft="16dp">
<FrameLayout
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginRight="3dp">
<ImageView
android:id="@+id/icon_circle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_circle" />
<ImageView
android:id="@+id/icon"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dp"
android:src="@drawable/badge"
android:tint="?android:attr/colorForeground" />
</FrameLayout>
<TextView
android:id="@+id/app_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="2dp"
android:singleLine="true"
android:text="Text"
android:textAppearance="@style/TextAppearance.AppCompat.Notification.Info"
android:textColor="?android:attr/colorForeground" />
<TextView
android:id="@+id/header_text_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:text="•"
android:textAppearance="@style/TextAppearance.AppCompat.Notification.Info"
android:textColor="?android:attr/colorForeground"
android:visibility="gone" />
<TextView
android:id="@+id/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:singleLine="true"
android:text="Text2"
android:textAppearance="@style/TextAppearance.AppCompat.Notification.Info"
android:textColor="?android:attr/colorForeground"
android:visibility="gone" />
<TextView
android:id="@+id/time_divider"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:singleLine="true"
android:text="Text3"
android:textAppearance="@style/TextAppearance.AppCompat.Notification.Info"
android:textColor="?android:attr/colorForeground"
android:visibility="gone" />
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.AppCompat.Notification.Time"
android:textColor="?android:attr/colorForeground"
android:visibility="gone" />
<Chronometer
android:id="@+id/chronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout="@layout/notification_template_part_chronometer"
android:visibility="gone" />
<ImageView
android:id="@+id/expand_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:contentDescription=""
android:paddingTop="1dp"
android:visibility="gone" />
<ImageView
android:id="@+id/profile_badge"
android:layout_width="12dp"
android:layout_height="12dp"
android:layout_gravity="center"
android:layout_marginLeft="4dp"
android:contentDescription=""
android:paddingTop="1dp"
android:scaleType="fitCenter"
android:visibility="gone" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView
android:id="@+id/notification_record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:contentDescription=""
android:scaleType="fitCenter"
android:src="@drawable/ic_fiber_manual_record_black_24dp"
android:tint="#ff0000"
android:visibility="visible" />
</LinearLayout>