OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 package org.appspot.apprtc; | 11 package org.appspot.apprtc; |
12 | 12 |
13 import android.app.Activity; | |
14 import android.app.AlertDialog; | |
15 import android.content.DialogInterface; | 13 import android.content.DialogInterface; |
16 import android.content.Intent; | 14 import android.content.Intent; |
17 import android.content.SharedPreferences; | 15 import android.content.SharedPreferences; |
18 import android.net.Uri; | 16 import android.net.Uri; |
19 import android.os.Bundle; | 17 import android.os.Bundle; |
20 import android.preference.PreferenceManager; | 18 import android.preference.PreferenceManager; |
19 import android.support.design.widget.FloatingActionButton; | |
AlexG
2016/05/20 20:29:08
Can we please remove this from sample app. It requ
| |
20 import android.support.v7.app.AlertDialog; | |
21 import android.support.v7.app.AppCompatActivity; | |
21 import android.util.Log; | 22 import android.util.Log; |
23 import android.view.ContextMenu; | |
22 import android.view.KeyEvent; | 24 import android.view.KeyEvent; |
25 import android.view.LayoutInflater; | |
23 import android.view.Menu; | 26 import android.view.Menu; |
24 import android.view.MenuItem; | 27 import android.view.MenuItem; |
25 import android.view.View; | 28 import android.view.View; |
26 import android.view.View.OnClickListener; | 29 import android.view.View.OnClickListener; |
27 import android.view.inputmethod.EditorInfo; | 30 import android.view.inputmethod.EditorInfo; |
28 import android.webkit.URLUtil; | 31 import android.webkit.URLUtil; |
29 import android.widget.AdapterView; | 32 import android.widget.AdapterView; |
30 import android.widget.ArrayAdapter; | 33 import android.widget.ArrayAdapter; |
31 import android.widget.EditText; | 34 import android.widget.EditText; |
32 import android.widget.ImageButton; | 35 import android.widget.ImageButton; |
33 import android.widget.ListView; | 36 import android.widget.ListView; |
34 import android.widget.TextView; | 37 import android.widget.TextView; |
35 | 38 |
36 import org.json.JSONArray; | 39 import org.json.JSONArray; |
37 import org.json.JSONException; | 40 import org.json.JSONException; |
38 | 41 |
39 import java.util.ArrayList; | 42 import java.util.ArrayList; |
40 import java.util.Random; | 43 import java.util.Random; |
41 | 44 |
42 /** | 45 /** |
43 * Handles the initial setup where the user selects which room to join. | 46 * Handles the initial setup where the user selects which room to join. |
44 */ | 47 */ |
45 public class ConnectActivity extends Activity { | 48 public class ConnectActivity extends AppCompatActivity { |
46 private static final String TAG = "ConnectActivity"; | 49 private static final String TAG = "ConnectActivity"; |
47 private static final int CONNECTION_REQUEST = 1; | 50 private static final int CONNECTION_REQUEST = 1; |
51 private static final int REMOVE_FAVORITE_INDEX = 0; | |
48 private static boolean commandLineRun = false; | 52 private static boolean commandLineRun = false; |
49 | 53 |
50 private ImageButton addRoomButton; | |
51 private ImageButton removeRoomButton; | |
52 private ImageButton connectButton; | 54 private ImageButton connectButton; |
53 private ImageButton connectLoopbackButton; | 55 private FloatingActionButton addFavoriteButton; |
54 private EditText roomEditText; | 56 private EditText roomEditText; |
55 private ListView roomListView; | 57 private ListView roomListView; |
56 private SharedPreferences sharedPref; | 58 private SharedPreferences sharedPref; |
57 private String keyprefVideoCallEnabled; | 59 private String keyprefVideoCallEnabled; |
58 private String keyprefResolution; | 60 private String keyprefResolution; |
59 private String keyprefFps; | 61 private String keyprefFps; |
60 private String keyprefCaptureQualitySlider; | 62 private String keyprefCaptureQualitySlider; |
61 private String keyprefVideoBitrateType; | 63 private String keyprefVideoBitrateType; |
62 private String keyprefVideoBitrateValue; | 64 private String keyprefVideoBitrateValue; |
63 private String keyprefVideoCodec; | 65 private String keyprefVideoCodec; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
107 | 109 |
108 setContentView(R.layout.activity_connect); | 110 setContentView(R.layout.activity_connect); |
109 | 111 |
110 roomEditText = (EditText) findViewById(R.id.room_edittext); | 112 roomEditText = (EditText) findViewById(R.id.room_edittext); |
111 roomEditText.setOnEditorActionListener( | 113 roomEditText.setOnEditorActionListener( |
112 new TextView.OnEditorActionListener() { | 114 new TextView.OnEditorActionListener() { |
113 @Override | 115 @Override |
114 public boolean onEditorAction( | 116 public boolean onEditorAction( |
115 TextView textView, int i, KeyEvent keyEvent) { | 117 TextView textView, int i, KeyEvent keyEvent) { |
116 if (i == EditorInfo.IME_ACTION_DONE) { | 118 if (i == EditorInfo.IME_ACTION_DONE) { |
117 addRoomButton.performClick(); | 119 addFavoriteButton.performClick(); |
118 return true; | 120 return true; |
119 } | 121 } |
120 return false; | 122 return false; |
121 } | 123 } |
122 }); | 124 }); |
123 roomEditText.requestFocus(); | 125 roomEditText.requestFocus(); |
124 | 126 |
125 roomListView = (ListView) findViewById(R.id.room_listview); | 127 roomListView = (ListView) findViewById(R.id.room_listview); |
126 roomListView.setChoiceMode(ListView.CHOICE_MODE_SINGLE); | 128 roomListView.setEmptyView(findViewById(android.R.id.empty)); |
127 | 129 roomListView.setOnItemClickListener(roomListClickListener); |
128 addRoomButton = (ImageButton) findViewById(R.id.add_room_button); | 130 registerForContextMenu(roomListView); |
129 addRoomButton.setOnClickListener(addRoomListener); | |
130 removeRoomButton = (ImageButton) findViewById(R.id.remove_room_button); | |
131 removeRoomButton.setOnClickListener(removeRoomListener); | |
132 connectButton = (ImageButton) findViewById(R.id.connect_button); | 131 connectButton = (ImageButton) findViewById(R.id.connect_button); |
133 connectButton.setOnClickListener(connectListener); | 132 connectButton.setOnClickListener(connectListener); |
134 connectLoopbackButton = | |
135 (ImageButton) findViewById(R.id.connect_loopback_button); | |
136 connectLoopbackButton.setOnClickListener(connectListener); | |
137 | 133 |
138 // If an implicit VIEW intent is launching the app, go directly to that URL. | 134 // If an implicit VIEW intent is launching the app, go directly to that URL. |
139 final Intent intent = getIntent(); | 135 final Intent intent = getIntent(); |
140 if ("android.intent.action.VIEW".equals(intent.getAction()) | 136 if ("android.intent.action.VIEW".equals(intent.getAction()) |
141 && !commandLineRun) { | 137 && !commandLineRun) { |
142 commandLineRun = true; | |
143 boolean loopback = intent.getBooleanExtra( | 138 boolean loopback = intent.getBooleanExtra( |
144 CallActivity.EXTRA_LOOPBACK, false); | 139 CallActivity.EXTRA_LOOPBACK, false); |
145 int runTimeMs = intent.getIntExtra( | 140 int runTimeMs = intent.getIntExtra( |
146 CallActivity.EXTRA_RUNTIME, 0); | 141 CallActivity.EXTRA_RUNTIME, 0); |
147 String room = sharedPref.getString(keyprefRoom, ""); | 142 String room = sharedPref.getString(keyprefRoom, ""); |
148 roomEditText.setText(room); | 143 connectToRoom(room, true, loopback, runTimeMs); |
149 connectToRoom(loopback, runTimeMs); | |
150 return; | 144 return; |
151 } | 145 } |
146 | |
147 addFavoriteButton = (FloatingActionButton) findViewById(R.id.add_favorite_bu tton); | |
148 addFavoriteButton.setOnClickListener(addFavoriteListener); | |
152 } | 149 } |
153 | 150 |
154 @Override | 151 @Override |
155 public boolean onCreateOptionsMenu(Menu menu) { | 152 public boolean onCreateOptionsMenu(Menu menu) { |
156 getMenuInflater().inflate(R.menu.connect_menu, menu); | 153 getMenuInflater().inflate(R.menu.connect_menu, menu); |
157 return true; | 154 return true; |
158 } | 155 } |
159 | 156 |
160 @Override | 157 @Override |
158 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextM enuInfo menuInfo) { | |
159 if (v.getId() == R.id.room_listview) { | |
160 AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuI nfo) menuInfo; | |
161 menu.setHeaderTitle(roomList.get(info.position)); | |
162 String[] menuItems = getResources().getStringArray(R.array.roomListContext Menu); | |
163 for (int i = 0; i < menuItems.length; i++) { | |
164 menu.add(Menu.NONE, i, i, menuItems[i]); | |
165 } | |
166 } else { | |
167 super.onCreateContextMenu(menu, v, menuInfo); | |
168 } | |
169 } | |
170 | |
171 @Override | |
172 public boolean onContextItemSelected(MenuItem item) { | |
173 if (item.getItemId() == REMOVE_FAVORITE_INDEX) { | |
174 AdapterView.AdapterContextMenuInfo info = | |
175 (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); | |
176 roomList.remove(info.position); | |
177 adapter.notifyDataSetChanged(); | |
178 return true; | |
179 } | |
180 | |
181 return super.onContextItemSelected(item); | |
182 } | |
183 | |
184 @Override | |
161 public boolean onOptionsItemSelected(MenuItem item) { | 185 public boolean onOptionsItemSelected(MenuItem item) { |
162 // Handle presses on the action bar items. | 186 // Handle presses on the action bar items. |
163 if (item.getItemId() == R.id.action_settings) { | 187 if (item.getItemId() == R.id.action_settings) { |
164 Intent intent = new Intent(this, SettingsActivity.class); | 188 Intent intent = new Intent(this, SettingsActivity.class); |
165 startActivity(intent); | 189 startActivity(intent); |
166 return true; | 190 return true; |
191 } else if (item.getItemId() == R.id.action_loopback) { | |
192 connectToRoom(null, false, true, 0); | |
193 return true; | |
167 } else { | 194 } else { |
168 return super.onOptionsItemSelected(item); | 195 return super.onOptionsItemSelected(item); |
169 } | 196 } |
170 } | 197 } |
171 | 198 |
172 @Override | 199 @Override |
173 public void onPause() { | 200 public void onPause() { |
174 super.onPause(); | 201 super.onPause(); |
175 String room = roomEditText.getText().toString(); | 202 String room = roomEditText.getText().toString(); |
176 String roomListJson = new JSONArray(roomList).toString(); | 203 String roomListJson = new JSONArray(roomList).toString(); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
210 protected void onActivityResult( | 237 protected void onActivityResult( |
211 int requestCode, int resultCode, Intent data) { | 238 int requestCode, int resultCode, Intent data) { |
212 if (requestCode == CONNECTION_REQUEST && commandLineRun) { | 239 if (requestCode == CONNECTION_REQUEST && commandLineRun) { |
213 Log.d(TAG, "Return: " + resultCode); | 240 Log.d(TAG, "Return: " + resultCode); |
214 setResult(resultCode); | 241 setResult(resultCode); |
215 commandLineRun = false; | 242 commandLineRun = false; |
216 finish(); | 243 finish(); |
217 } | 244 } |
218 } | 245 } |
219 | 246 |
220 private final OnClickListener connectListener = new OnClickListener() { | 247 private void connectToRoom( |
221 @Override | 248 String roomId, boolean commandLineRun, boolean loopback, int runTimeMs) { |
222 public void onClick(View view) { | 249 this.commandLineRun = commandLineRun; |
223 boolean loopback = false; | |
224 if (view.getId() == R.id.connect_loopback_button) { | |
225 loopback = true; | |
226 } | |
227 commandLineRun = false; | |
228 connectToRoom(loopback, 0); | |
229 } | |
230 }; | |
231 | 250 |
232 private void connectToRoom(boolean loopback, int runTimeMs) { | 251 // roomId is random for loopback. |
233 // Get room name (random for loopback). | |
234 String roomId; | |
235 if (loopback) { | 252 if (loopback) { |
236 roomId = Integer.toString((new Random()).nextInt(100000000)); | 253 roomId = Integer.toString((new Random()).nextInt(100000000)); |
237 } else { | |
238 roomId = getSelectedItem(); | |
239 if (roomId == null) { | |
240 roomId = roomEditText.getText().toString(); | |
241 } | |
242 } | 254 } |
243 | 255 |
244 String roomUrl = sharedPref.getString( | 256 String roomUrl = sharedPref.getString( |
245 keyprefRoomServerUrl, | 257 keyprefRoomServerUrl, |
246 getString(R.string.pref_room_server_url_default)); | 258 getString(R.string.pref_room_server_url_default)); |
247 | 259 |
248 // Video call enabled flag. | 260 // Video call enabled flag. |
249 boolean videoCallEnabled = sharedPref.getBoolean(keyprefVideoCallEnabled, | 261 boolean videoCallEnabled = sharedPref.getBoolean(keyprefVideoCallEnabled, |
250 Boolean.valueOf(getString(R.string.pref_videocall_default))); | 262 Boolean.valueOf(getString(R.string.pref_videocall_default))); |
251 | 263 |
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
383 .setMessage(getString(R.string.invalid_url_text, url)) | 395 .setMessage(getString(R.string.invalid_url_text, url)) |
384 .setCancelable(false) | 396 .setCancelable(false) |
385 .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() { | 397 .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() { |
386 public void onClick(DialogInterface dialog, int id) { | 398 public void onClick(DialogInterface dialog, int id) { |
387 dialog.cancel(); | 399 dialog.cancel(); |
388 } | 400 } |
389 }).create().show(); | 401 }).create().show(); |
390 return false; | 402 return false; |
391 } | 403 } |
392 | 404 |
393 private final OnClickListener addRoomListener = new OnClickListener() { | 405 private final AdapterView.OnItemClickListener |
406 roomListClickListener = new AdapterView.OnItemClickListener() { | |
394 @Override | 407 @Override |
395 public void onClick(View view) { | 408 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l ) { |
396 String newRoom = roomEditText.getText().toString(); | 409 String roomId = ((TextView) view).getText().toString(); |
397 if (newRoom.length() > 0 && !roomList.contains(newRoom)) { | 410 connectToRoom(roomId, false, false, 0); |
398 adapter.add(newRoom); | |
399 adapter.notifyDataSetChanged(); | |
400 } | |
401 } | 411 } |
402 }; | 412 }; |
403 | 413 |
404 private final OnClickListener removeRoomListener = new OnClickListener() { | 414 private final OnClickListener addFavoriteListener = new OnClickListener() { |
405 @Override | 415 @Override |
406 public void onClick(View view) { | 416 public void onClick(View view) { |
407 String selectedRoom = getSelectedItem(); | 417 AlertDialog.Builder builder = new AlertDialog.Builder(ConnectActivity.this ); |
408 if (selectedRoom != null) { | 418 |
409 adapter.remove(selectedRoom); | 419 final View dialogView = LayoutInflater.from(ConnectActivity.this) |
410 adapter.notifyDataSetChanged(); | 420 .inflate(R.layout.dialog_add_favorite, null); |
411 } | 421 final EditText favoriteEditText = (EditText) dialogView.findViewById(R.id. favorite_edittext); |
422 favoriteEditText.append(roomEditText.getText()); | |
423 | |
424 builder.setTitle(R.string.add_favorite_title) | |
425 .setPositiveButton(R.string.add, new DialogInterface.OnClickListener() { | |
426 @Override | |
427 public void onClick(DialogInterface dialogInterface, int i) { | |
428 String newRoom = favoriteEditText.getText().toString(); | |
429 if (newRoom.length() > 0 && !roomList.contains(newRoom)) { | |
430 adapter.add(newRoom); | |
431 adapter.notifyDataSetChanged(); | |
432 } | |
433 } | |
434 }) | |
435 .setView(dialogView) | |
436 .setNegativeButton(R.string.cancel, null); | |
437 builder.show(); | |
412 } | 438 } |
413 }; | 439 }; |
414 | 440 |
415 private String getSelectedItem() { | 441 private final OnClickListener connectListener = new OnClickListener() { |
416 int position = AdapterView.INVALID_POSITION; | 442 @Override |
417 if (roomListView.getCheckedItemCount() > 0 && adapter.getCount() > 0) { | 443 public void onClick(View view) { |
418 position = roomListView.getCheckedItemPosition(); | 444 connectToRoom(roomEditText.getText().toString(), false, false, 0); |
419 if (position >= adapter.getCount()) { | |
420 position = AdapterView.INVALID_POSITION; | |
421 } | |
422 } | 445 } |
423 if (position != AdapterView.INVALID_POSITION) { | 446 }; |
424 return adapter.getItem(position); | |
425 } else { | |
426 return null; | |
427 } | |
428 } | |
429 | |
430 } | 447 } |
OLD | NEW |