Skip to content

Commit

Permalink
#7 Feat: Use MaterialDrawer library for the drawer layout
Browse files Browse the repository at this point in the history
  • Loading branch information
ditek committed Jul 20, 2019
1 parent 7b3128b commit 3861cbd
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 111 deletions.
9 changes: 8 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ dependencies {
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'jp.kshoji:midi-driver:0.1.5:@aar'
implementation 'androidx.preference:preference:1.1.0-beta01'
implementation 'androidx.preference:preference:1.1.0-rc01'
implementation 'com.android.support:design:28.0.0'

// For the navigation drawer
implementation "com.mikepenz:materialdrawer:7.0.0-rc01"
implementation "androidx.recyclerview:recyclerview:1.0.0"
implementation "androidx.annotation:annotation:1.1.0"
implementation "com.google.android.material:material:1.0.0"
implementation "androidx.constraintlayout:constraintlayout:1.1.3"
}
65 changes: 36 additions & 29 deletions app/src/main/java/com/midisheetmusic/MidiPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,32 @@

package com.midisheetmusic;

import java.util.*;
import java.io.*;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.view.*;
import android.widget.*;
import android.os.*;
import android.media.*;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.SystemClock;
import android.util.AttributeSet;

import androidx.drawerlayout.widget.DrawerLayout;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.midisheetmusic.sheets.ChordSymbol;
import com.midisheetmusic.sheets.MusicSymbol;
import com.mikepenz.materialdrawer.Drawer;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;

/**
* The MidiPlayer is the panel at the top used to play the sound
Expand Down Expand Up @@ -58,19 +69,15 @@
* and determines which notes to shade.
*/
public class MidiPlayer extends LinearLayout {
private ImageButton backButton; /** The back button */
private ImageButton rewindButton; /** The rewind button */
private ImageButton playButton; /** The play/pause button */
private ImageButton stopButton; /** The stop button */
private ImageButton fastFwdButton; /** The fast forward button */
private ImageButton settingsButton; /** The fast forward button */
private Button midiButton;
private Button leftHandButton;
private Button rightHandButton;
private ImageButton pianoButton;
private TextView speedText; /** The "Speed %" label */
private SeekBar speedBar; /** The seekbar for controlling the playback speed */
private DrawerLayout drawerLayout;
/** The "Speed %" label */
private TextView speedText;
/** The seekbar for controlling playback speed */
private SeekBar speedBar;
private Drawer drawer;

/** The index corresponding to left/right hand in the track list */
private static final int LEFT_TRACK = 1;
Expand Down Expand Up @@ -168,16 +175,16 @@ void OnMidiNote(int note, boolean pressed) {
void init() {
inflate(activity, R.layout.player_toolbar, this);

backButton = findViewById(R.id.btn_back);
rewindButton = findViewById(R.id.btn_rewind);
stopButton = findViewById(R.id.btn_replay);
playButton = findViewById(R.id.btn_play);
fastFwdButton = findViewById(R.id.btn_forward);
ImageButton backButton = findViewById(R.id.btn_back);
ImageButton rewindButton = findViewById(R.id.btn_rewind);
ImageButton stopButton = findViewById(R.id.btn_replay);
ImageButton playButton = findViewById(R.id.btn_play);
ImageButton fastFwdButton = findViewById(R.id.btn_forward);
ImageButton settingsButton = findViewById(R.id.btn_settings);
leftHandButton = findViewById(R.id.btn_left);
rightHandButton = findViewById(R.id.btn_right);
midiButton = findViewById(R.id.btn_midi);
pianoButton = findViewById(R.id.btn_piano);
settingsButton = findViewById(R.id.btn_settings);
speedText = findViewById(R.id.txt_speed);
speedBar = findViewById(R.id.speed_bar);

Expand All @@ -186,11 +193,11 @@ void init() {
stopButton.setOnClickListener(v -> Stop());
playButton.setOnClickListener(v -> Play());
fastFwdButton.setOnClickListener(v -> FastForward());
settingsButton.setOnClickListener(v -> drawer.openDrawer());
midiButton.setOnClickListener(v -> toggleMidi());
leftHandButton.setOnClickListener(v -> toggleTrack(LEFT_TRACK));
rightHandButton.setOnClickListener(v -> toggleTrack(RIGHT_TRACK));
pianoButton.setOnClickListener(v -> togglePiano());
settingsButton.setOnClickListener(v -> drawerLayout.openDrawer(Gravity.RIGHT));

speedBar.getProgressDrawable().setColorFilter(Color.parseColor("#00BB87"), PorterDuff.Mode.SRC_IN);
speedBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
Expand Down Expand Up @@ -711,8 +718,8 @@ public boolean isInMidiMode() {
return playstate == midi;
}

public void setDrawerLayout(DrawerLayout drawerLayout) {
this.drawerLayout = drawerLayout;
public void setDrawer(Drawer drawer) {
this.drawer = drawer;
}
}

Expand Down
87 changes: 30 additions & 57 deletions app/src/main/java/com/midisheetmusic/SheetMusicActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
package com.midisheetmusic;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
Expand All @@ -23,20 +22,19 @@
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.drawerlayout.widget.DrawerLayout;

import com.google.android.material.navigation.NavigationView;
import com.midisheetmusic.sheets.ClefSymbol;
import com.mikepenz.materialdrawer.Drawer;
import com.mikepenz.materialdrawer.DrawerBuilder;
import com.mikepenz.materialdrawer.model.interfaces.IDrawerItem;

import java.io.File;
import java.io.FileOutputStream;
Expand All @@ -53,8 +51,7 @@
* <li> Piano : For highlighting the piano notes during playback.
* <li> SheetMusic : For highlighting the sheet music notes during playback.
*/
public class SheetMusicActivity extends MidiHandlingActivity
implements NavigationView.OnNavigationItemSelectedListener {
public class SheetMusicActivity extends MidiHandlingActivity {

public static final String MidiTitleID = "MidiTitleID";
public static final int settingsRequestCode = 1;
Expand All @@ -65,12 +62,10 @@ public class SheetMusicActivity extends MidiHandlingActivity
private LinearLayout layout; /* The layout */
private MidiFile midifile; /* The midi file to play */
private MidiOptions options; /* The options for sheet music and sound */
private long midiCRC; /* CRC of the midi bytes */
private long midiCRC; /* CRC of the midi bytes */
private Drawer drawer;

private DrawerLayout drawerLayout;
private NavigationView navigationView;

/** Create this SheetMusicActivity.
/** Create this SheetMusicActivity.
* The Intent should have two parameters:
* - data: The uri of the midi file to open.
* - MidiTitleID: The title of the song (String)
Expand Down Expand Up @@ -133,15 +128,20 @@ public void onCreate(Bundle state) {
void createViews() {
layout = findViewById(R.id.sheet_content);

drawerLayout = findViewById(R.id.drawer_layout);
// Drawer
drawer = new DrawerBuilder()
.withActivity(this)
.inflateMenu(R.menu.sheet_menu)
.withOnDrawerItemClickListener((view, i, item) -> drawerItemClickListener(item))
.withDrawerGravity(Gravity.RIGHT)
.build();
// Make sure that the view extends over the navigation buttons area
drawer.getDrawerLayout().setFitsSystemWindows(false);
// Lock the drawer so swiping doesn't open it
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
drawer.getDrawerLayout().setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

player = new MidiPlayer(this);
player.setDrawerLayout(drawerLayout);
player.setDrawer(drawer);
layout.addView(player);

piano = new Piano(this);
Expand Down Expand Up @@ -181,35 +181,18 @@ public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

/** When the menu button is pressed, initialize the menus. */
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (player != null) {
player.Pause();
}
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.sheet_menu, menu);
return true;
}

/** Callback when a menu item is selected.
* - Choose Song : Choose a new song
* - Song Settings : Adjust the sheet music and sound options
* - Save As Images: Save the sheet music as PNG images
* - Help : Display the HTML help screen
*/
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
/** Handle clicks on the drawer menu */
public boolean drawerItemClickListener(IDrawerItem item) {
switch ((int)item.getIdentifier()) {
case R.id.song_settings:
changeSettings();
break;
case R.id.save_images:
showSaveImagesDialog();
break;
}
// menuItem.setChecked(true);
drawerLayout.closeDrawers();
item.setSelected(false);
drawer.closeDrawer();
return true;
}

Expand Down Expand Up @@ -238,14 +221,10 @@ private void showSaveImagesDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.save_images_str);
builder.setView(dialogView);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface builder, int whichButton) {
saveAsImages(filenameView.getText().toString());
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface builder, int whichButton) {
}
builder.setPositiveButton("OK",
(builder1, whichButton) -> saveAsImages(filenameView.getText().toString()));
builder.setNegativeButton("Cancel",
(builder12, whichButton) -> {
});
AlertDialog dialog = builder.create();
dialog.show();
Expand Down Expand Up @@ -286,21 +265,15 @@ private void saveAsImages(String name) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Error saving image to file " + Environment.DIRECTORY_PICTURES + "/MidiSheetMusic/" + filename + ".png");
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setPositiveButton("OK", (dialog, id) -> { });
AlertDialog alert = builder.create();
alert.show();
}
catch (NullPointerException e) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Ran out of memory while saving image to file " + Environment.DIRECTORY_PICTURES + "/MidiSheetMusic/" + filename + ".png");
builder.setCancelable(false);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
builder.setPositiveButton("OK", (dialog, id) -> {});
AlertDialog alert = builder.create();
alert.show();
}
Expand Down
26 changes: 3 additions & 23 deletions app/src/main/res/layout/sheet_music_layout.xml
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/sheet_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="left"
android:fitsSystemWindows="false">

<LinearLayout
android:id="@+id/sheet_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />

<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:fitsSystemWindows="true"
tools:layout_gravity="left"
app:menu="@menu/sheet_menu" />

</androidx.drawerlayout.widget.DrawerLayout>
android:orientation="vertical" />
3 changes: 2 additions & 1 deletion app/src/main/res/values/styles.xml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="Theme.AppCompat.NoActionBar.Fullscreen" parent="@style/Theme.AppCompat.NoActionBar">
<style name="Theme.AppCompat.NoActionBar.Fullscreen" parent="@style/MaterialDrawerTheme">
<item name="android:windowNoTitle">true</item>
<item name="android:windowActionBar">false</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">@null</item>
<item name="colorAccent">@color/md_teal_300</item>
</style>

<style name="AppTheme" parent="@style/Theme.AppCompat.NoActionBar"/>
Expand Down

0 comments on commit 3861cbd

Please sign in to comment.