-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
VisualizerActivity.java
128 lines (112 loc) · 4.93 KB
/
VisualizerActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package android.example.com.visualizerpreferences;
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import android.Manifest;
import android.content.pm.PackageManager;
import android.example.com.visualizerpreferences.AudioVisuals.AudioInputReader;
import android.example.com.visualizerpreferences.AudioVisuals.VisualizerView;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class VisualizerActivity extends AppCompatActivity {
private static final int MY_PERMISSION_RECORD_AUDIO_REQUEST_CODE = 88;
private VisualizerView mVisualizerView;
private AudioInputReader mAudioInputReader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_visualizer);
mVisualizerView = (VisualizerView) findViewById(R.id.activity_visualizer);
defaultSetup();
setupPermissions();
}
private void defaultSetup() {
mVisualizerView.setShowBass(true);
mVisualizerView.setShowMid(true);
mVisualizerView.setShowTreble(true);
mVisualizerView.setMinSizeScale(1);
mVisualizerView.setColor(getString(R.string.pref_color_red_value));
}
/**
* Below this point is code you do not need to modify; it deals with permissions
* and starting/cleaning up the AudioInputReader
**/
/**
* onPause Cleanup audio stream
**/
@Override
protected void onPause() {
super.onPause();
if (mAudioInputReader != null) {
mAudioInputReader.shutdown(isFinishing());
}
}
@Override
protected void onResume() {
super.onResume();
if (mAudioInputReader != null) {
mAudioInputReader.restart();
}
}
/**
* App Permissions for Audio
**/
private void setupPermissions() {
// If we don't have the record audio permission...
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) != PackageManager.PERMISSION_GRANTED) {
// And if we're on SDK M or later...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// Ask again, nicely, for the permissions.
String[] permissionsWeNeed = new String[]{ Manifest.permission.RECORD_AUDIO };
requestPermissions(permissionsWeNeed, MY_PERMISSION_RECORD_AUDIO_REQUEST_CODE);
}
} else {
// Otherwise, permissions were granted and we are ready to go!
mAudioInputReader = new AudioInputReader(mVisualizerView, this);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String permissions[], @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSION_RECORD_AUDIO_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// The permission was granted! Start up the visualizer!
mAudioInputReader = new AudioInputReader(mVisualizerView, this);
} else {
Toast.makeText(this, "Permission for audio not granted. Visualizer can't run.", Toast.LENGTH_LONG).show();
finish();
// The permission was denied, so we can show a message why we can't run the app
// and then close the app.
}
}
// Other permissions could go down here
}
}
// TODO (1) Create a new Empty Activity named SettingsActivity; make sure to generate the
// activity_settings.xml layout file as well and add the activity to the manifest
// TODO (2) Add a new resource folder called menu and create visualizer_menu.xml
// TODO (3) In visualizer_menu.xml create a menu item with a single item. The id should be
// "action_settings", title should be saved in strings.xml, the item should never
// be shown as an action, and orderInCategory should be 100
// TODO (5) Add the menu to the menu bar
// TODO (6) When the "Settings" menu item is pressed, open SettingsActivity
}