|
@@ -0,0 +1,349 @@
|
|
|
+package com.example.cloudymiusic;
|
|
|
+
|
|
|
+import androidx.annotation.NonNull;
|
|
|
+import androidx.appcompat.app.AppCompatActivity;
|
|
|
+import androidx.core.app.ActivityCompat;
|
|
|
+import androidx.core.content.ContextCompat;
|
|
|
+import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
+import androidx.recyclerview.widget.RecyclerView;
|
|
|
+//import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
|
|
|
+
|
|
|
+import android.Manifest;
|
|
|
+import android.annotation.SuppressLint;
|
|
|
+import android.app.Activity;
|
|
|
+import android.app.AlertDialog;
|
|
|
+import android.app.DownloadManager;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.DialogInterface;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.pm.PackageManager;
|
|
|
+import android.graphics.Color;
|
|
|
+import android.net.Uri;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.CountDownTimer;
|
|
|
+import android.os.Handler;
|
|
|
+import android.os.HandlerThread;
|
|
|
+import android.os.Message;
|
|
|
+import android.provider.Settings;
|
|
|
+import android.util.Log;
|
|
|
+import android.view.LayoutInflater;
|
|
|
+import android.view.View;
|
|
|
+import android.view.ViewGroup;
|
|
|
+import android.widget.ImageView;
|
|
|
+import android.widget.LinearLayout;
|
|
|
+import android.widget.TextView;
|
|
|
+import android.widget.Toast;
|
|
|
+
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collection;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+import adapter.MyAdapter;
|
|
|
+import bean.SongListItem;
|
|
|
+import okhttp3.Call;
|
|
|
+import okhttp3.Response;
|
|
|
+import util.OkHttpHelper;
|
|
|
+
|
|
|
+public class MainActivity extends AppCompatActivity implements View.OnClickListener {
|
|
|
+ private static final int REQUEST_CODE = 100;
|
|
|
+ private String[] permissions;
|
|
|
+ private boolean isAllGrant;
|
|
|
+ //开启权限
|
|
|
+
|
|
|
+ private LinearLayout button_recommended;
|
|
|
+ private TextView button_text1;
|
|
|
+ private LinearLayout button_find;
|
|
|
+ private TextView button_text2;
|
|
|
+ private LinearLayout button_manyou;
|
|
|
+ private TextView button_text3;
|
|
|
+ private LinearLayout button_dongtai;
|
|
|
+ private TextView button_text4;
|
|
|
+ private LinearLayout button_mine;
|
|
|
+ private TextView button_text5;
|
|
|
+ private RecyclerView recommended_rv;
|
|
|
+
|
|
|
+
|
|
|
+ private List<SongListItem> mLists;
|
|
|
+ private MyAdapter adapter;
|
|
|
+ private HandlerThread handlerThread;
|
|
|
+ private Handler mHandler , uiHandler;
|
|
|
+
|
|
|
+// private SwipeRefreshLayout mSwiperRefreshLayout;
|
|
|
+ //消息的类型,分别为初始状态、下拉刷新状态、上拉加载状态
|
|
|
+ private final int MSG_INIT = 0x11;
|
|
|
+ private final int MSG_REFRESH = 0x12;
|
|
|
+ private final int MSG_TYPE = 0x13;
|
|
|
+ private int page = 1;
|
|
|
+
|
|
|
+
|
|
|
+ @SuppressLint("HandlerLeak")
|
|
|
+ @Override
|
|
|
+ protected void onCreate(Bundle savedInstanceState) {
|
|
|
+ super.onCreate(savedInstanceState);
|
|
|
+ setContentView(R.layout.activity_main);
|
|
|
+ View view = LayoutInflater.from(this).inflate(R.layout.activity_main,null);
|
|
|
+ recommended_rv = findViewById(R.id.recommended_rv);
|
|
|
+ permissions = new String[]{
|
|
|
+ Manifest.permission.INTERNET,
|
|
|
+ Manifest.permission.ACCESS_NETWORK_STATE,
|
|
|
+
|
|
|
+ };
|
|
|
+
|
|
|
+ initUI();
|
|
|
+ initListener();
|
|
|
+ normal(view);
|
|
|
+
|
|
|
+ recommended_rv = findViewById(R.id.recommended_rv);
|
|
|
+ mLists = new ArrayList<>();
|
|
|
+ adapter = MyAdapter.getInstance(mLists,this);
|
|
|
+ LinearLayoutManager manager = new LinearLayoutManager(this);
|
|
|
+ manager.setOrientation(RecyclerView.HORIZONTAL);
|
|
|
+ recommended_rv.setLayoutManager(manager);
|
|
|
+ recommended_rv.setAdapter(adapter);
|
|
|
+
|
|
|
+
|
|
|
+ //消息的处理
|
|
|
+ mHandler = new Handler() {
|
|
|
+ @Override
|
|
|
+ public void handleMessage(@NonNull Message msg) {
|
|
|
+ super.handleMessage(msg);
|
|
|
+ switch (msg.what) {
|
|
|
+ case 1:
|
|
|
+ Log.e("handleMessage: ",msg.obj+"" );
|
|
|
+ mLists.addAll((Collection<? extends SongListItem>) msg.obj);
|
|
|
+ Log.e("handleMessage", mLists.size()+"");
|
|
|
+ adapter.notifyDataSetChanged();
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ Log.e("handleMessage", mLists.size()+"q");
|
|
|
+ }
|
|
|
+
|
|
|
+ public void normal(View view){
|
|
|
+ isAllGrant = checkIsAllPermissionGranted(permissions,this);
|
|
|
+ if (isAllGrant){
|
|
|
+ //已有权限 -> 做事
|
|
|
+ Toast.makeText(this,"已有权限",Toast.LENGTH_SHORT).show();
|
|
|
+ doWork();
|
|
|
+ }else{
|
|
|
+ //没有权限 -> 请求权限
|
|
|
+ ActivityCompat.requestPermissions(this,permissions,REQUEST_CODE);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+// public void frame(View view){
|
|
|
+// startActivity(new Intent(this);
|
|
|
+// }
|
|
|
+// private void initView(){
|
|
|
+//// mSwiperRefreshLayout = findViewById(R.id.swiper_refresh_layout);
|
|
|
+// mSwiperRefreshLayout.setColorSchemeColors(Color.RED);
|
|
|
+// mSwiperRefreshLayout.setProgressBackgroundColorSchemeColor(Color.BLUE);
|
|
|
+// mSwiperRefreshLayout.setDistanceToTriggerSync(300);
|
|
|
+// mSwiperRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
|
|
|
+//
|
|
|
+//
|
|
|
+// }
|
|
|
+
|
|
|
+ private void doWork() {
|
|
|
+ //todo 有权限时,想做的事
|
|
|
+ Log.e("asd", "doWork: " );
|
|
|
+ OkHttpHelper okHttpHelper = OkHttpHelper.getInstance();
|
|
|
+ okHttpHelper.getRequest("http://192.168.43.198:3000/personalized?limit=5", new OkHttpHelper.OkHttpInterface() {
|
|
|
+ @Override
|
|
|
+ public void failure(Call call, IOException e) {
|
|
|
+ Log.d("failure: ", e.getMessage());
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void success(Call call, Response response) {
|
|
|
+ try{
|
|
|
+ String body = response.body().string();
|
|
|
+ JSONObject data = new JSONObject(body);
|
|
|
+ JSONArray results = data.optJSONArray("result");
|
|
|
+ for (int i = 0;i < results.length(); i++){
|
|
|
+ SongListItem songListItem = new SongListItem();
|
|
|
+ JSONObject item = (JSONObject) results.get(i);
|
|
|
+ songListItem.setId(item.optString("id"));
|
|
|
+ songListItem.setName(item.optString("name"));
|
|
|
+ songListItem.setPicUrl(item.optString("picUrl"));
|
|
|
+ songListItem.setTrackNumberUpdateTime(item.optLong("trackNumberUpdateTime",200));
|
|
|
+ Log.d("success: ",results.get(i).toString());
|
|
|
+ mLists.add(songListItem);
|
|
|
+ }
|
|
|
+ Message msg = mHandler.obtainMessage();
|
|
|
+ msg.what = 1;
|
|
|
+ msg.obj = mLists;
|
|
|
+ mHandler.sendMessage(msg);
|
|
|
+ Log.d( "success: ",mLists.get(0).getName() + mLists.size());
|
|
|
+ }catch (Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+ //permission 权限数组
|
|
|
+ //权限都已授权返回true,否则false
|
|
|
+ //检查所有权限是否授权
|
|
|
+ private boolean checkIsAllPermissionGranted(String[] permissions , Context context) {
|
|
|
+ //遍历检测权限
|
|
|
+ for (String permission : permissions) {
|
|
|
+ if (ContextCompat.checkSelfPermission(context,permission) != PackageManager.PERMISSION_GRANTED){
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ //权限回调处理,如果用户此时还拒绝,则提示他去手机的设置->管理界面打开权限
|
|
|
+ @Override
|
|
|
+ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
|
|
|
+ super.onRequestPermissionsResult(requestCode, permissions, grantResults);
|
|
|
+ if (requestCode == REQUEST_CODE){
|
|
|
+ isAllGrant = true;
|
|
|
+ for (int grant : grantResults) {
|
|
|
+ if (grant == PackageManager.PERMISSION_GRANTED){
|
|
|
+ isAllGrant = false;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }if (isAllGrant){ //同意授权 -> 做事情
|
|
|
+ Toast.makeText(this,"授权成功",Toast.LENGTH_SHORT).show();
|
|
|
+ doWork();
|
|
|
+ }else{
|
|
|
+ //拒绝授权 -> 开弹窗询问是否跳设置 -> 权限管理界面
|
|
|
+ AlertDialog.Builder builder = new AlertDialog.Builder(this)
|
|
|
+ .setMessage("需要您的网络权限,请到设置-权限管理中授权。")
|
|
|
+ .setPositiveButton("确定", new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
+ Intent intent = new Intent();
|
|
|
+ intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
|
|
|
+ intent.addCategory(Intent.CATEGORY_DEFAULT);
|
|
|
+ intent.setData(Uri.parse("package:" + getPackageName()));
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
|
|
|
+ intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
|
|
|
+ startActivity(intent);
|
|
|
+ }
|
|
|
+ }).setCancelable(false)
|
|
|
+ .setNegativeButton("取消", new DialogInterface.OnClickListener() {
|
|
|
+ @Override
|
|
|
+ public void onClick(DialogInterface dialogInterface, int i) {
|
|
|
+ Toast.makeText(MainActivity.this,"您没有获得此权限,此功能无法正常使用",Toast.LENGTH_SHORT).show();
|
|
|
+ }
|
|
|
+ });
|
|
|
+ builder.create().show();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initUI(){
|
|
|
+ button_recommended = findViewById(R.id.button_recommended);
|
|
|
+ button_text1 = findViewById(R.id.button_text1);
|
|
|
+
|
|
|
+ button_find = findViewById(R.id.button_find);
|
|
|
+ button_text2 = findViewById(R.id.button_text2);
|
|
|
+
|
|
|
+ button_manyou = findViewById(R.id.button_manyou);
|
|
|
+ button_text3 = findViewById(R.id.button_text3);
|
|
|
+
|
|
|
+ button_dongtai = findViewById(R.id.button_dongtai);
|
|
|
+ button_text4 = findViewById(R.id.button_text4);
|
|
|
+
|
|
|
+ button_mine = findViewById(R.id.button_mine);
|
|
|
+ button_text5 = findViewById(R.id.button_text5);
|
|
|
+
|
|
|
+ recommended_rv = findViewById(R.id.recommended_rv);
|
|
|
+
|
|
|
+ findViewById(R.id.button_img1).setSelected(true);
|
|
|
+ button_text1.setTextColor(0xFFFF0000);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ private void initListener(){
|
|
|
+ button_recommended.setOnClickListener(this);
|
|
|
+ button_find.setOnClickListener(this);
|
|
|
+ button_manyou.setOnClickListener(this);
|
|
|
+ button_dongtai.setOnClickListener(this);
|
|
|
+ button_mine.setOnClickListener(this);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void onClick(View view) {
|
|
|
+ switch (view.getId()) {
|
|
|
+ case R.id.button_recommended:
|
|
|
+ findViewById(R.id.button_img1).setSelected(true);
|
|
|
+ button_text1.setTextColor(0xFFFF0000);
|
|
|
+
|
|
|
+ findViewById(R.id.button_img2).setSelected(false);
|
|
|
+ button_text2.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img3).setSelected(false);
|
|
|
+ button_text3.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img4).setSelected(false);
|
|
|
+ button_text4.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img5).setSelected(false);
|
|
|
+ button_text5.setTextColor(0xFF000000);
|
|
|
+ break;
|
|
|
+ case R.id.button_find:
|
|
|
+ findViewById(R.id.button_img2).setSelected(true);
|
|
|
+ button_text2.setTextColor(0xFFFF0000);
|
|
|
+
|
|
|
+ findViewById(R.id.button_img1).setSelected(false);
|
|
|
+ button_text1.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img3).setSelected(false);
|
|
|
+ button_text3.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img4).setSelected(false);
|
|
|
+ button_text4.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img5).setSelected(false);
|
|
|
+ button_text5.setTextColor(0xFF000000);
|
|
|
+ break;
|
|
|
+ case R.id.button_manyou:
|
|
|
+ findViewById(R.id.button_img3).setSelected(true);
|
|
|
+ button_text3.setTextColor(0xFFFF0000);
|
|
|
+
|
|
|
+ findViewById(R.id.button_img2).setSelected(false);
|
|
|
+ button_text2.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img1).setSelected(false);
|
|
|
+ button_text1.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img4).setSelected(false);
|
|
|
+ button_text4.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img5).setSelected(false);
|
|
|
+ button_text5.setTextColor(0xFF000000);
|
|
|
+ break;
|
|
|
+ case R.id.button_dongtai:
|
|
|
+ findViewById(R.id.button_img4).setSelected(true);
|
|
|
+ button_text4.setTextColor(0xFFFF0000);
|
|
|
+
|
|
|
+ findViewById(R.id.button_img3).setSelected(false);
|
|
|
+ button_text3.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img2).setSelected(false);
|
|
|
+ button_text2.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img1).setSelected(false);
|
|
|
+ button_text1.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img5).setSelected(false);
|
|
|
+ button_text5.setTextColor(0xFF000000);
|
|
|
+ break;
|
|
|
+ case R.id.button_mine:
|
|
|
+ findViewById(R.id.button_img5).setSelected(true);
|
|
|
+ button_text5.setTextColor(0xFFFF0000);
|
|
|
+
|
|
|
+ findViewById(R.id.button_img1).setSelected(false);
|
|
|
+ button_text1.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img2).setSelected(false);
|
|
|
+ button_text2.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img3).setSelected(false);
|
|
|
+ button_text3.setTextColor(0xFF000000);
|
|
|
+ findViewById(R.id.button_img4).setSelected(false);
|
|
|
+ button_text4.setTextColor(0xFF000000);
|
|
|
+
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|