You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
233 lines
9.3 KiB
233 lines
9.3 KiB
package com.example.musicwork.freeModel;
|
|
|
|
import android.app.AlertDialog;
|
|
import android.app.Dialog;
|
|
import android.content.DialogInterface;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.graphics.Color;
|
|
import android.os.Environment;
|
|
import android.provider.MediaStore;
|
|
//import android.support.annotation.IdRes;
|
|
//import android.support.v4.app.NotificationCompat;
|
|
//import android.support.v4.view.LayoutInflaterCompat;
|
|
//import android.support.v7.app.AlertDialog;
|
|
//import android.support.v7.app.AppCompatActivity;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.LayoutInflater;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.LinearLayout;
|
|
import android.widget.RadioButton;
|
|
import android.widget.RadioGroup;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import androidx.core.app.ActivityCompat;
|
|
|
|
import com.example.musicwork.Data;
|
|
import com.example.musicwork.R;
|
|
import com.example.musicwork.SignIn;
|
|
|
|
import org.json.JSONException;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.DataInputStream;
|
|
import java.io.File;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
import java.io.OutputStream;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class InitActivity extends AppCompatActivity {
|
|
|
|
String username ;
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_init);
|
|
final Data data = (Data)getApplication();
|
|
username = data.getUsername();
|
|
btnInit();
|
|
}
|
|
|
|
//进行动态权限申请
|
|
private static final int REQUEST_EXTERNAL_STORAGE = 1;
|
|
private static String[] PERMISSIONS_STORAGE = {
|
|
"android.permission.READ_EXTERNAL_STORAGE",
|
|
"android.permission.WRITE_EXTERNAL_STORAGE" };
|
|
|
|
public void verifyStoragePermissions() {
|
|
try {
|
|
//检测是否有写的权限
|
|
int permission = ActivityCompat.checkSelfPermission(InitActivity.this,
|
|
"android.permission.READ_EXTERNAL_STORAGE");
|
|
if (permission != PackageManager.PERMISSION_GRANTED) {
|
|
// 没有写的权限,去申请写的权限,会弹出对话框
|
|
ActivityCompat.requestPermissions(InitActivity.this, PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE);
|
|
}
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
//创建弹窗,显示文件名
|
|
public void showFileName(ArrayList<String> list){
|
|
LinearLayout fileList = new LinearLayout(this);
|
|
fileList.setOrientation(LinearLayout.VERTICAL);
|
|
TextView hintText = new TextView(this);
|
|
hintText.setText("请选择想要打开的音乐文件:");
|
|
hintText.setTextColor(Color.BLACK);
|
|
hintText.setTextSize(15);
|
|
fileList.addView(hintText);
|
|
|
|
Dialog dialog = new Dialog(this);
|
|
for(int i=0;i<list.size();i++){
|
|
TextView fileName = new TextView(this);
|
|
fileName.setText(list.get(i));
|
|
|
|
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
|
|
params.setMargins(10,5,0,5);
|
|
fileName.setLayoutParams(params);
|
|
|
|
fileList.addView(fileName);
|
|
|
|
//监听
|
|
fileName.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
TextView textView = (TextView)v;
|
|
String fileName = textView.getText().toString();
|
|
try {
|
|
if(FileServiceDownload.downloadForm(fileName)){
|
|
// String string = sentFile(fileName);
|
|
Toast.makeText(getApplicationContext(),"下载文件成功",Toast.LENGTH_SHORT).show();
|
|
|
|
sentFile(fileName);
|
|
//Log.e("Init content:",string);
|
|
}else{
|
|
Toast.makeText(getApplicationContext(),"下载文件失败",Toast.LENGTH_SHORT).show();
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
//创建弹窗
|
|
|
|
dialog.setContentView(fileList);
|
|
dialog.show();
|
|
}
|
|
|
|
|
|
|
|
//将文件内容传入主活动
|
|
public void sentFile(String filename) throws IOException {
|
|
verifyStoragePermissions();
|
|
|
|
Bundle bundle = new Bundle();
|
|
Intent intent = new Intent(InitActivity.this,FreemdActivity.class);//传递输入数据
|
|
bundle.putString("flag","open");
|
|
|
|
List<MusicNote> noteList = new ArrayList<>();
|
|
String path=Environment.getExternalStorageDirectory().getPath()+"/Music/"+filename;
|
|
String environment = Environment.getExternalStorageState();
|
|
if(Environment.MEDIA_MOUNTED.equals(environment)) {
|
|
//读文件
|
|
File file = new File(path);
|
|
|
|
FileInputStream fis = new FileInputStream(file);
|
|
Log.e("filepath:", file.getPath());
|
|
DataInputStream in = new DataInputStream(fis);
|
|
//StringBuilder content = new StringBuilder();
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
|
|
String line;
|
|
String mydata = "";
|
|
int i = 0;
|
|
while ((line = reader.readLine()) != null) {
|
|
String[] strarray = line.split(" ");
|
|
bundle.putStringArray("note"+i,strarray);
|
|
i += 1;
|
|
//mydata = mydata + line;
|
|
/*int note = Integer.parseInt(strarray[0]);
|
|
float beat = Float.parseFloat(strarray[1]);
|
|
MusicNote musicNote = new MusicNote(note,beat);
|
|
noteList.add(musicNote);*/
|
|
//Log.e("mydata:", " "+strarray[1]);
|
|
}
|
|
bundle.putInt("length",i);//存储读出的音符列表的长度
|
|
}
|
|
//将数据发送到主活动
|
|
intent.putExtras(bundle);
|
|
startActivity(intent);
|
|
}
|
|
|
|
private void btnInit(){
|
|
Button createMusic = (Button)findViewById(R.id.create_music);
|
|
Button openMusic = (Button)findViewById(R.id.open_score);
|
|
|
|
//打开文件按钮的监听
|
|
openMusic.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
ArrayList<String> list = new ArrayList<String>();
|
|
try {
|
|
list=CheckFileList.CheckFileList(SignIn.author_name);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} catch (JSONException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
showFileName(list);//显示文件列表
|
|
}
|
|
});
|
|
|
|
|
|
//新建按钮的监听
|
|
createMusic.setOnClickListener(new View.OnClickListener() {
|
|
@Override
|
|
public void onClick(View v) {
|
|
//弹出窗口
|
|
final Intent intent = new Intent(InitActivity.this,FreemdActivity.class);//传递输入数据
|
|
LayoutInflater inflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
|
|
final View view = inflater.inflate(R.layout.create_layout,null);//加载新建界面布局
|
|
AlertDialog.Builder builder = new AlertDialog.Builder(InitActivity.this);//创建弹出框
|
|
builder.setView(view);//设置弹出框界面
|
|
builder.setTitle("新建简谱").setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
|
@Override
|
|
public void onClick(DialogInterface dialog, int which) {//接收输入框输入的信息
|
|
final Bundle bundle = new Bundle();
|
|
|
|
EditText musicNameView = (EditText)view.findViewById(R.id.music_name);
|
|
String musicName = musicNameView.getText().toString();//音乐名
|
|
|
|
final RadioGroup musicBeatGroup = (RadioGroup)view.findViewById(R.id.music_beat);
|
|
RadioButton musicBeatBtn = (RadioButton)view.findViewById(musicBeatGroup.getCheckedRadioButtonId());//被选中的单选按钮
|
|
String musicBeat = musicBeatBtn.getText().toString();//音乐拍号
|
|
//存入bundle
|
|
bundle.putString("flag","init");
|
|
bundle.putString("musicName",musicName);
|
|
bundle.putString("musicBeat",musicBeat);
|
|
|
|
intent.putExtras(bundle);
|
|
dialog.cancel();//关闭弹出框
|
|
startActivity(intent);
|
|
}
|
|
}).create().show();
|
|
|
|
}
|
|
});
|
|
}
|
|
}
|