package org.ntlab.irisclient; import android.annotation.SuppressLint; import android.content.DialogInterface; import android.content.ClipData; import android.content.ClipDescription; import android.content.ClipboardManager; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CompoundButton; import android.widget.ImageButton; import android.widget.ListView; import android.widget.Switch; import android.widget.TextView; import com.google.android.material.snackbar.Snackbar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.app.AppCompatActivity; import androidx.lifecycle.Observer; import androidx.lifecycle.ViewModelProvider; import org.ntlab.irisclient.models.Member; import org.ntlab.irisclient.viewmodels.RoomViewModel; import java.util.ArrayList; import java.util.List; public class OwnerRoomActivity extends AppCompatActivity { //フィールド ArrayList<String> viewMembers = new ArrayList<>(); ArrayList<String> oldMembers = new ArrayList<>(); List<Member> allMembers = new ArrayList<>(); List<Integer> Mteam = new ArrayList<>(); List<Integer> Steam = new ArrayList<>(); List<Integer> Rteam = new ArrayList<>(); List<Integer> Bteam = new ArrayList<>(); String roomId = "null"; String myName = "null"; boolean drawingTimer = true; int drawingTimes = 40; boolean gameTimer = true; int gameTimes = 90; int gameFirstTimes = 10; private void homeButton(RoomViewModel roomViewModel){ String strTitle = "ホーム画面に戻ってもよろしいですか?"; String strMessage = "退出すると部屋は削除されます"; AlertDialog.Builder builder; builder = new AlertDialog.Builder(this); builder.setMessage(strMessage); builder.setTitle(strTitle); builder.setPositiveButton("ok", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { roomViewModel.ownerLeaving(roomId , myName); Intent i = new Intent(OwnerRoomActivity.this, MainActivity.class); startActivity(i); } }); builder.setNegativeButton("cancel", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create(); builder.show(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_owner_room); myName = ((Iris) getApplication()).getNickname(); roomId = ((Iris) getApplication()).getRid(); //-------------------------------------------------------------------------- //メンバー表示 Member mn = new Member(myName); mn.setBelongs("g"); mn.setMaster(false); viewMembers.add(mn.getNickname()); //RoomViewModelへのアクセス RoomViewModel roomViewModel = new ViewModelProvider(this).get(RoomViewModel.class); roomViewModel.setRid(roomId); ListView membersList = (ListView) findViewById(R.id.MembersList); BaseAdapter adapter = new MemberListAdapter(this.getApplicationContext(), R.layout.member_list_layout, viewMembers, allMembers); membersList.setAdapter(adapter); //LiveData(List<Member>)への購読 roomViewModel.getMembersLiveData().observe(this, new Observer<List<Member>>() { private List<Member> members; @Override public void onChanged(List<Member> irisMembers) { viewMembers.clear(); irisMembers.forEach(m -> viewMembers.add(m.getNickname())); allMembers = irisMembers; Member myInfo = allMembers.get(myNameIndex(allMembers,myName)); ((Iris) getApplication()).setMaster(myInfo.isMaster()); ((Iris) getApplication()).setTeam(myInfo.getBelongs()); BaseAdapter adapter = new MemberListAdapter(getApplicationContext(), R.layout.member_list_layout, viewMembers, allMembers); membersList.setAdapter(adapter); } }); //各ボタン処理 ArrayList<String> members = new ArrayList<>(); Button redMasterButton = (Button) findViewById(R.id.beRedMaster); Button redSpyButton = (Button) findViewById(R.id.beRedSpy); Button blueMasterButton = (Button) findViewById(R.id.beBlueMaster); Button blueSpyButton = (Button) findViewById(R.id.beBlueSpy); ImageButton copyButton = (ImageButton) findViewById(R.id.copyButton); ImageButton homeButton = (ImageButton) findViewById(R.id.homeButton); @SuppressLint("UseSwitchCompatOrMaterialCode") Switch dTimer = findViewById(R.id.dTimer); @SuppressLint("UseSwitchCompatOrMaterialCode") Switch gTimer = findViewById(R.id.gTimer); @SuppressLint("UseSwitchCompatOrMaterialCode") Switch d20Times = findViewById(R.id.d20Times); @SuppressLint("UseSwitchCompatOrMaterialCode") Switch d40Times = findViewById(R.id.d40Times); @SuppressLint("UseSwitchCompatOrMaterialCode") Switch d60Times = findViewById(R.id.d60Times); @SuppressLint("UseSwitchCompatOrMaterialCode") Switch g60Times = findViewById(R.id.g60Times); @SuppressLint("UseSwitchCompatOrMaterialCode") Switch g90Times = findViewById(R.id.g90Times); @SuppressLint("UseSwitchCompatOrMaterialCode") Switch g120Times = findViewById(R.id.g120Times); //OwnerRoom専用ボタン Button randomButton = (Button) findViewById(R.id.random); Button startButton = (Button) findViewById(R.id.start); //チームと役職の変更 //赤チーム・スパイマスター redMasterButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { roomViewModel.changeBelongsAndMaster(roomId, myName,"r",true); } }); //赤チーム・諜報員 redSpyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { roomViewModel.changeBelongsAndMaster(roomId, myName,"r",false); } }); //青チーム・スパイマスター blueMasterButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { roomViewModel.changeBelongsAndMaster(roomId, myName,"b",true); } }); //青チーム・諜報員 blueSpyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { roomViewModel.changeBelongsAndMaster(roomId, myName,"b",false); } }); //コピーボタン copyButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Snackbar.make(v, "コピーしました", Snackbar.LENGTH_LONG).show(); CopyClipBoard(); CreateCommonIntent(); } }); //設定ボタン //お絵描きタイマー dTimer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { drawingTimer = isChecked; if(drawingTimer == false){ d20Times.setVisibility(View.GONE); d40Times.setVisibility(View.GONE); d60Times.setVisibility(View.GONE); }else { d20Times.setVisibility(View.VISIBLE); d40Times.setVisibility(View.VISIBLE); d60Times.setVisibility(View.VISIBLE); } roomViewModel.changeSettings(roomId, drawingTimer, drawingTimes, gameTimer, gameTimes, gameFirstTimes); } }); //d20タイマー d20Times.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { d40Times.setChecked(false); d60Times.setChecked(false); drawingTimes = 20; roomViewModel.changeSettings(roomId, drawingTimer, drawingTimes, gameTimer, gameTimes, gameFirstTimes); } }); //d40タイマー d40Times.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { d20Times.setChecked(false); d60Times.setChecked(false); drawingTimes = 40; roomViewModel.changeSettings(roomId, drawingTimer, drawingTimes, gameTimer, gameTimes, gameFirstTimes); } }); //d60タイマー d60Times.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { d20Times.setChecked(false); d40Times.setChecked(false); drawingTimes = 60; roomViewModel.changeSettings(roomId, drawingTimer, drawingTimes, gameTimer, gameTimes, gameFirstTimes); } }); //ゲームタイマー gTimer.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { gameTimer = isChecked; if(gameTimer == false){ g60Times.setVisibility(View.GONE); g90Times.setVisibility(View.GONE); g120Times.setVisibility(View.GONE); }else { g60Times.setVisibility(View.VISIBLE); g90Times.setVisibility(View.VISIBLE); g120Times.setVisibility(View.VISIBLE); } roomViewModel.changeSettings(roomId, drawingTimer, drawingTimes, gameTimer, gameTimes, gameFirstTimes); } }); //g60タイマー g60Times.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { g90Times.setChecked(false); g120Times.setChecked(false); gameTimes = 60; roomViewModel.changeSettings(roomId, drawingTimer, drawingTimes, gameTimer, gameTimes, gameFirstTimes); } }); //g90タイマー g90Times.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { g60Times.setChecked(false); g120Times.setChecked(false); gameTimes = 90; roomViewModel.changeSettings(roomId, drawingTimer, drawingTimes, gameTimer, gameTimes, gameFirstTimes); } }); //g120タイマー g120Times.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { g60Times.setChecked(false); g90Times.setChecked(false); gameTimes = 120; roomViewModel.changeSettings(roomId, drawingTimer, drawingTimes, gameTimer, gameTimes, gameFirstTimes); } }); //ランダムボタン randomButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { roomViewModel.randomTeam(roomId,myName); } }); //スタートボタン startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if(ConditionStart()){ roomViewModel.startDrawing(roomId, myName); Intent intent = new Intent(OwnerRoomActivity.this,DrawingActivity.class); startActivity(intent); }else{ Snackbar.make(v, "スタート条件を満たしていません", Snackbar.LENGTH_LONG).show(); } } }); //ホームボタン homeButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { homeButton(roomViewModel); } }); //部屋ID情報 TextView RoomIdText = findViewById(R.id.roomID); RoomIdText.setText(roomId); roomViewModel.start(500,(Iris)getApplication()); } public void CopyClipBoard() { String copyText = roomId; // 作成したレース結果を取得 // クリップボードへの格納成功時は成功メッセージをトーストで表示 boolean result = SetClipData(copyText); if(result) { } else { } } private boolean SetClipData(String copyText) { try { //クリップボードに格納するItemを作成 ClipData.Item item = new ClipData.Item(copyText); //MIMETYPEの作成 String[] mimeType = new String[1]; mimeType[0] = ClipDescription.MIMETYPE_TEXT_URILIST; //クリップボードに格納するClipDataオブジェクトの作成 ClipData cd = new ClipData(new ClipDescription("text_data", mimeType), item); //クリップボードにデータを格納 ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); cm.setPrimaryClip(cd); return true; } catch (Exception e) { return false; } } private void CreateCommonIntent() { //システムのクリップボードを取得 ClipboardManager cm = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE); //クリップボードからClipDataを取得 ClipData cd = cm.getPrimaryClip(); if(cd != null) { // クリップボードにコピーがある場合 ClipData.Item item = cd.getItemAt(0); Intent sendIntent =new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.setType("text/plain"); sendIntent.putExtra(sendIntent.EXTRA_TEXT, item.getText()); // メモ帳のテキスト欄、メールアプリの本文にテキストをセット startActivity(sendIntent); // 共有メニューを開く } } //スタートを押したときの人数・チーム・役職状況 public boolean ConditionStart() { Mteam.clear(); Steam.clear(); Rteam.clear(); Bteam.clear(); for(int i=0; i< allMembers.size(); i++){ if(allMembers.get(i).getMaster()){ Mteam.add(i); }else{ Steam.add(i); } if(allMembers.get(i).getBelongs().equals("r")){ Rteam.add(i); }else if(allMembers.get(i).getBelongs().equals("b")) { Bteam.add(i); } else { return false; } } if(Mteam.size() != 2){ return false; } if(Rteam.size() < 2){ return false; } if(Bteam.size() < 2){ return false; } return true; } private int myNameIndex(List<Member> list, String name){ for(int i = 0; i < list.size(); i++){ if(list.get(i).getNickname().equals(name)) { return i; } } return 0; } }