Java tutorial
/* * Copyright 2014 The WebRTC Project Authors. All rights reserved. * * Use of this source code is governed by a BSD-style license * that can be found in the LICENSE file in the root of the source * tree. An additional intellectual property rights grant can be found * in the file PATENTS. All contributing project authors may * be found in the AUTHORS file in the root of the source tree. */ //-------Code edited by Saikat Pakira @ Esolz on 8th June,2016 package sp.esolz.connex.admin.apprtc; import android.Manifest; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Bundle; import android.preference.PreferenceManager; import android.support.v4.app.ActivityCompat; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.webkit.URLUtil; import android.widget.Toast; import com.android.volley.Request; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.JsonObjectRequest; import org.json.JSONObject; import sp.esolz.connex.admin.BaseActivity.ParentActivity; import sp.esolz.connex.admin.R; import sp.esolz.connex.admin.appliaction.ConnexApplication; import sp.esolz.connex.admin.appliaction.ConnexConstante; /** * Handles the initial setup where the user selects which room to join. */ public class ConnectActivity extends ParentActivity { final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1; public static final String ACTION_APP = "ACTION_APP"; public static final String ACTION_NOTIFICATION = "ACTION_NOTIFICATION"; private static final String TAG = "ConnectActivity"; private static final int CONNECTION_REQUEST = 1; private static boolean commandLineRun = false; private SharedPreferences sharedPref; private String keyprefVideoCallEnabled; private String keyprefResolution; private String keyprefFps; private String keyprefCaptureQualitySlider; private String keyprefVideoBitrateType; private String keyprefVideoBitrateValue; private String keyprefVideoCodec; private String keyprefAudioBitrateType; private String keyprefAudioBitrateValue; private String keyprefAudioCodec; private String keyprefHwCodecAcceleration; private String keyprefCaptureToTexture; private String keyprefNoAudioProcessingPipeline; private String keyprefAecDump; private String keyprefOpenSLES; private String keyprefDisplayHud; private String keyprefTracing; private String keyprefRoomServerUrl; private String keyprefRoom; private String keyprefRoomList; //================Added Code of Saikat on 8th June private String ROOM_NAME = ""; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get setting keys. PreferenceManager.setDefaultValues(this, R.xml.preferences, false); sharedPref = PreferenceManager.getDefaultSharedPreferences(this); keyprefVideoCallEnabled = getString(R.string.pref_videocall_key); keyprefResolution = getString(R.string.pref_resolution_key); keyprefFps = getString(R.string.pref_fps_key); keyprefCaptureQualitySlider = getString(R.string.pref_capturequalityslider_key); keyprefVideoBitrateType = getString(R.string.pref_startvideobitrate_key); keyprefVideoBitrateValue = getString(R.string.pref_startvideobitratevalue_key); keyprefVideoCodec = getString(R.string.pref_videocodec_key); keyprefHwCodecAcceleration = getString(R.string.pref_hwcodec_key); keyprefCaptureToTexture = getString(R.string.pref_capturetotexture_key); keyprefAudioBitrateType = getString(R.string.pref_startaudiobitrate_key); keyprefAudioBitrateValue = getString(R.string.pref_startaudiobitratevalue_key); keyprefAudioCodec = getString(R.string.pref_audiocodec_key); keyprefNoAudioProcessingPipeline = getString(R.string.pref_noaudioprocessing_key); keyprefAecDump = getString(R.string.pref_aecdump_key); keyprefOpenSLES = getString(R.string.pref_opensles_key); keyprefDisplayHud = getString(R.string.pref_displayhud_key); keyprefTracing = getString(R.string.pref_tracing_key); keyprefRoomServerUrl = getString(R.string.pref_room_server_url_key); keyprefRoom = getString(R.string.pref_room_key); keyprefRoomList = getString(R.string.pref_room_list_key); setContentView(R.layout.activity_connect); //=============permission management permissionChk(); } public void permissionChk() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_EXTERNAL_STORAGE) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.MODIFY_AUDIO_SETTINGS) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.RECORD_AUDIO) && ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) { ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA, Manifest.permission.MODIFY_AUDIO_SETTINGS, Manifest.permission.RECORD_AUDIO }, MY_PERMISSIONS_REQUEST_READ_CONTACTS); } else { // No explanation needed, we can request the permission. ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.MODIFY_AUDIO_SETTINGS, Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO }, MY_PERMISSIONS_REQUEST_READ_CONTACTS); } return; } else { if (getIntent().getAction().equals(ACTION_APP)) { fetchMeetingList(); } else { ROOM_NAME = getIntent().getStringExtra("ROOM_NAME"); connectToRoom(0); } } } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { if (grantResults.length > 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED && grantResults[2] == PackageManager.PERMISSION_GRANTED && grantResults[3] == PackageManager.PERMISSION_GRANTED && grantResults[4] == PackageManager.PERMISSION_GRANTED) { if (getIntent().getAction().equals(ACTION_APP)) { fetchMeetingList(); } else { ROOM_NAME = getIntent().getStringExtra("ROOM_NAME"); connectToRoom(0); } } else { android.support.v7.app.AlertDialog.Builder alertDialogBuilder = new android.support.v7.app.AlertDialog.Builder( ConnectActivity.this); alertDialogBuilder.setMessage( "With out these permissions, we cant move into meeting room. Would you like to continue?"); alertDialogBuilder.setPositiveButton("Retry", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { permissionChk(); } }); alertDialogBuilder.setNegativeButton("DISCARD", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { finish(); } }); android.support.v7.app.AlertDialog alertDialog = alertDialogBuilder.create(); alertDialog.show(); } return; } } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.connect_menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items. if (item.getItemId() == R.id.action_settings) { Intent intent = new Intent(this, SettingsActivity.class); startActivity(intent); return true; } else { return super.onOptionsItemSelected(item); } } @Override public void onPause() { super.onPause(); SharedPreferences.Editor editor = sharedPref.edit(); editor.putString(keyprefRoom, ROOM_NAME); editor.commit(); } @Override public void onResume() { super.onResume(); ROOM_NAME = sharedPref.getString(keyprefRoom, ""); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == CONNECTION_REQUEST && commandLineRun) { Log.d(TAG, "Return: " + resultCode); setResult(resultCode); commandLineRun = false; finish(); } } //============main calling method private void connectToRoom(int runTimeMs) { // Get room name (random for loopback). String roomId = ROOM_NAME; String roomUrl = sharedPref.getString(keyprefRoomServerUrl, getString(R.string.pref_room_server_url_default)); // Video call enabled flag. boolean videoCallEnabled = sharedPref.getBoolean(keyprefVideoCallEnabled, Boolean.valueOf(getString(R.string.pref_videocall_default))); // Get default codecs. String videoCodec = sharedPref.getString(keyprefVideoCodec, getString(R.string.pref_videocodec_default)); String audioCodec = sharedPref.getString(keyprefAudioCodec, getString(R.string.pref_audiocodec_default)); // Check HW codec flag. boolean hwCodec = sharedPref.getBoolean(keyprefHwCodecAcceleration, Boolean.valueOf(getString(R.string.pref_hwcodec_default))); // Check Capture to texture. boolean captureToTexture = sharedPref.getBoolean(keyprefCaptureToTexture, Boolean.valueOf(getString(R.string.pref_capturetotexture_default))); // Check Disable Audio Processing flag. boolean noAudioProcessing = sharedPref.getBoolean(keyprefNoAudioProcessingPipeline, Boolean.valueOf(getString(R.string.pref_noaudioprocessing_default))); // Check Disable Audio Processing flag. boolean aecDump = sharedPref.getBoolean(keyprefAecDump, Boolean.valueOf(getString(R.string.pref_aecdump_default))); // Check OpenSL ES enabled flag. boolean useOpenSLES = sharedPref.getBoolean(keyprefOpenSLES, Boolean.valueOf(getString(R.string.pref_opensles_default))); // Get video resolution from settings. int videoWidth = 0; int videoHeight = 0; String resolution = sharedPref.getString(keyprefResolution, getString(R.string.pref_resolution_default)); String[] dimensions = resolution.split("[ x]+"); if (dimensions.length == 2) { try { videoWidth = Integer.parseInt(dimensions[0]); videoHeight = Integer.parseInt(dimensions[1]); } catch (NumberFormatException e) { videoWidth = 0; videoHeight = 0; Log.e(TAG, "Wrong video resolution setting: " + resolution); } } // Get camera fps from settings. int cameraFps = 0; String fps = sharedPref.getString(keyprefFps, getString(R.string.pref_fps_default)); String[] fpsValues = fps.split("[ x]+"); if (fpsValues.length == 2) { try { cameraFps = Integer.parseInt(fpsValues[0]); } catch (NumberFormatException e) { Log.e(TAG, "Wrong camera fps setting: " + fps); } } // Check capture quality slider flag. boolean captureQualitySlider = sharedPref.getBoolean(keyprefCaptureQualitySlider, Boolean.valueOf(getString(R.string.pref_capturequalityslider_default))); // Get video and audio start bitrate. int videoStartBitrate = 0; String bitrateTypeDefault = getString(R.string.pref_startvideobitrate_default); String bitrateType = sharedPref.getString(keyprefVideoBitrateType, bitrateTypeDefault); if (!bitrateType.equals(bitrateTypeDefault)) { String bitrateValue = sharedPref.getString(keyprefVideoBitrateValue, getString(R.string.pref_startvideobitratevalue_default)); videoStartBitrate = Integer.parseInt(bitrateValue); } int audioStartBitrate = 0; bitrateTypeDefault = getString(R.string.pref_startaudiobitrate_default); bitrateType = sharedPref.getString(keyprefAudioBitrateType, bitrateTypeDefault); if (!bitrateType.equals(bitrateTypeDefault)) { String bitrateValue = sharedPref.getString(keyprefAudioBitrateValue, getString(R.string.pref_startaudiobitratevalue_default)); audioStartBitrate = Integer.parseInt(bitrateValue); } // Check statistics display option. boolean displayHud = sharedPref.getBoolean(keyprefDisplayHud, Boolean.valueOf(getString(R.string.pref_displayhud_default))); boolean tracing = sharedPref.getBoolean(keyprefTracing, Boolean.valueOf(getString(R.string.pref_tracing_default))); // Start AppRTCDemo activity. Log.d(TAG, "Connecting to room " + roomId + " at URL " + roomUrl); if (validateUrl(roomUrl)) { Uri uri = Uri.parse(roomUrl); Intent intent = new Intent(this, CallActivity.class); intent.setData(uri); intent.putExtra(CallActivity.EXTRA_ROOMID, roomId); intent.putExtra(CallActivity.EXTRA_LOOPBACK, false); intent.putExtra(CallActivity.EXTRA_VIDEO_CALL, videoCallEnabled); intent.putExtra(CallActivity.EXTRA_VIDEO_WIDTH, videoWidth); intent.putExtra(CallActivity.EXTRA_VIDEO_HEIGHT, videoHeight); intent.putExtra(CallActivity.EXTRA_VIDEO_FPS, cameraFps); intent.putExtra(CallActivity.EXTRA_VIDEO_CAPTUREQUALITYSLIDER_ENABLED, captureQualitySlider); intent.putExtra(CallActivity.EXTRA_VIDEO_BITRATE, videoStartBitrate); intent.putExtra(CallActivity.EXTRA_VIDEOCODEC, videoCodec); intent.putExtra(CallActivity.EXTRA_HWCODEC_ENABLED, hwCodec); intent.putExtra(CallActivity.EXTRA_CAPTURETOTEXTURE_ENABLED, captureToTexture); intent.putExtra(CallActivity.EXTRA_NOAUDIOPROCESSING_ENABLED, noAudioProcessing); intent.putExtra(CallActivity.EXTRA_AECDUMP_ENABLED, aecDump); intent.putExtra(CallActivity.EXTRA_OPENSLES_ENABLED, useOpenSLES); intent.putExtra(CallActivity.EXTRA_AUDIO_BITRATE, audioStartBitrate); intent.putExtra(CallActivity.EXTRA_AUDIOCODEC, audioCodec); intent.putExtra(CallActivity.EXTRA_DISPLAY_HUD, displayHud); intent.putExtra(CallActivity.EXTRA_TRACING, tracing); intent.putExtra(CallActivity.EXTRA_CMDLINE, commandLineRun); intent.putExtra(CallActivity.EXTRA_RUNTIME, runTimeMs); startActivityForResult(intent, CONNECTION_REQUEST); } } private boolean validateUrl(String url) { if (URLUtil.isHttpsUrl(url) || URLUtil.isHttpUrl(url)) { return true; } new AlertDialog.Builder(this).setTitle(getText(R.string.invalid_url_title)) .setMessage(getString(R.string.invalid_url_text, url)).setCancelable(false) .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }).create().show(); return false; } public void fetchMeetingList() { final String URL = ConnexConstante.DOMAIN_URL + "Meeting/meetnow?duration=" + ConnexConstante.MEET_NOW_DURATION + "&admin_id=8&loggedinId=" + ConnexApplication.getInstance().getUserID(); Log.i(TAG, URL); JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, URL, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.i(TAG, response.toString()); try { if (response.getString("status").equalsIgnoreCase("success")) { ROOM_NAME = response.getString("meeting_id"); connectToRoom(0); } else { Toast.makeText(ConnectActivity.this, "Failed to create Meeting, Try Again!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(ConnectActivity.this, "Failed to create Meeting, Try Again!", Toast.LENGTH_SHORT).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.i(TAG, "Error: " + error.getMessage()); Toast.makeText(ConnectActivity.this, "Failed to create Meeting, Try Again!", Toast.LENGTH_SHORT).show(); } }); ConnexApplication.getInstance().addToRequestQueue(jsonObjReq); } }