Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 21.78 KB | None | 0 0
  1. /*
  2. * ************************************************************************
  3. * <p/>
  4. * <p/>
  5. * Copyright (c) 2019 RT-RK
  6. * Bulevar Narodnog Fronta 23a
  7. * <p/>
  8. * All Rights Reserved
  9. * <p/>
  10. * P R O P R I E T A R Y & C O N F I D E N T I A L
  11. * <p/>
  12. * -----------------------------------------------------
  13. * http://www.rt-rk.com
  14. * -----------------------------------------------------
  15. * <p/>
  16. * <p/>
  17. * <p/>
  18. *
  19. * @author Skywalker
  20. * <p/>
  21. * <p/>
  22. * ************************************************************************
  23. */
  24. package com.rtrk.automotive.skywalker.skyplayer.app.home;
  25.  
  26. import android.annotation.SuppressLint;
  27. import android.app.AlertDialog;
  28. import android.content.Context;
  29. import android.content.DialogInterface;
  30. import android.content.Intent;
  31. import android.content.res.Configuration;
  32. import android.graphics.Color;
  33. import android.graphics.Typeface;
  34. import android.location.LocationListener;
  35. import android.location.LocationManager;
  36. import android.os.Build;
  37. import android.os.Bundle;
  38. import android.text.Spannable;
  39. import android.text.SpannableString;
  40. import android.util.Log;
  41. import android.util.TypedValue;
  42. import android.view.Gravity;
  43. import android.view.LayoutInflater;
  44. import android.view.Menu;
  45. import android.view.MenuItem;
  46. import android.view.SubMenu;
  47. import android.view.View;
  48. import android.view.WindowManager;
  49. import android.widget.AdapterView;
  50. import android.widget.ListView;
  51. import android.widget.RelativeLayout;
  52. import android.widget.TextView;
  53.  
  54. import androidx.appcompat.app.ActionBarDrawerToggle;
  55. import androidx.appcompat.app.AppCompatActivity;
  56. import androidx.appcompat.widget.Toolbar;
  57. import androidx.drawerlayout.widget.DrawerLayout;
  58. import androidx.fragment.app.Fragment;
  59.  
  60. import com.google.android.libraries.places.api.model.Place;
  61. import com.google.android.material.navigation.NavigationView;
  62. import com.rtrk.automotive.skywalker.skyplayer.R;
  63. import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.FragmentMessage;
  64. import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.IFragmentCallback;
  65. import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.IPTVFragment;
  66. import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.PickDestinationFragment;
  67. import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.QoSOptionsFragment;
  68. import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.RouteDisplayFragment;
  69. import com.rtrk.automotive.skywalker.skyplayer.app.login.LoginActivity;
  70. import com.rtrk.automotive.skywalker.skyplayer.player.MediaPlayerController;
  71. import com.rtrk.automotive.skywalker.skyplayer.player.tv.TVControls;
  72. import com.rtrk.automotive.skywalker.skyplayer.server.DatabaseController;
  73. import com.rtrk.automotive.skywalker.skyplayer.server.RouteInfo;
  74. import com.rtrk.automotive.skywalker.skyplayer.server.SkyLocationListener;
  75. import com.rtrk.automotive.skywalker.skyplayer.server.model.AppUser;
  76. import com.rtrk.automotive.skywalker.skyplayer.strategies.BufferStrategy;
  77. import com.rtrk.automotive.skywalker.skyplayer.strategies.model.Event;
  78. import com.rtrk.automotive.skywalker.skyplayer.strategies.model.EventQueue;
  79. import com.rtrk.automotive.skywalker.skyplayer.strategies.model.EventType;
  80. import com.rtrk.automotive.skywalker.skyplayer.strategies.model.Parameter;
  81. import com.rtrk.automotive.skywalker.skyplayer.strategies.model.ParameterQueue;
  82. import com.rtrk.automotive.skywalker.skyplayer.strategies.model.QueueElement;
  83. import com.rtrk.automotive.skywalker.skyplayer.utils.Constants;
  84. import com.rtrk.automotive.skywalker.skyplayer.utils.CustomTypeFont;
  85. import com.rtrk.automotive.skywalker.skyplayer.utils.Utils;
  86.  
  87. import java.io.File;
  88. import java.net.URISyntaxException;
  89.  
  90.  
  91. public class HomeActivity extends AppCompatActivity {
  92. private static final boolean DEBUG = Constants.Debug.DEBUG_HOME;
  93. private static final String TAG = HomeActivity.class.getSimpleName();
  94.  
  95. //TODO: Make instance of data collection controller when implementation is done
  96. private IPTVFragment mIpTVFragment;
  97. private PickDestinationFragment mPickDestinationFragment;
  98. private IFragmentCallback mRouteDisplayCallback;
  99. private IFragmentCallback mQosOptionsCallback;
  100. private LocationListener mSkyLocationListener = null;
  101. private LocationManager mLocationManager = null;
  102.  
  103. /**
  104. * Used to hide/show sidebar that's part of the navigation bar.
  105. */
  106. private DrawerLayout mDrawerLayout;
  107.  
  108. private NavigationView navigationView;
  109. private View navHeader;
  110. private RelativeLayout headerLayout;
  111.  
  112. /**
  113. * ListView used to display channel list
  114. */
  115. private ListView mListView;
  116.  
  117. /**
  118. * {@link TVControls} used for channel switching
  119. */
  120. private TVControls mTVControls;
  121. /**
  122. * Controller for database used in application
  123. */
  124. private DatabaseController mDatabaseController;
  125.  
  126. @Override
  127. public void onConfigurationChanged(Configuration newConfig) {
  128. super.onConfigurationChanged(newConfig);
  129.  
  130. //Remove all existing items from the menu, leaving it empty as if it had just been created.
  131. prepareNavigationView();
  132. if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  133. getSupportActionBar().show();
  134. } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  135. getSupportActionBar().hide();
  136. getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  137. }
  138. }
  139.  
  140. @SuppressLint("MissingPermission")
  141. @Override
  142. protected void onCreate(Bundle savedInstanceState) {
  143. super.onCreate(savedInstanceState);
  144. setContentView(R.layout.activity_home);
  145.  
  146. mSkyLocationListener = new SkyLocationListener(getApplicationContext());
  147.  
  148. MediaPlayerController.getInstance().init(this);
  149. MediaPlayerController.getInstance().setStrategy(new BufferStrategy());
  150. mDatabaseController = new DatabaseController(this);
  151. navigationView = findViewById(R.id.nav_view);
  152. navHeader = navigationView.getHeaderView(Constants.HomeScreenConstants.HOME_NAVIGATION_VIEW_DEFAULT_POSITION);
  153. headerLayout = navHeader.findViewById(R.id.headers_layout);
  154.  
  155. mPickDestinationFragment = new PickDestinationFragment();
  156. mIpTVFragment = new IPTVFragment();
  157.  
  158. setChannelListOnClick();
  159.  
  160. prepareFragments();
  161.  
  162. /**
  163. * Location manager that listens to location change
  164. */
  165. mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
  166. if (mLocationManager != null) {
  167. mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Constants.SkyLocationListenerConstants.MINIMUM_TIME_INTERVAL_MILLIS, Constants.SkyLocationListenerConstants.MINIMUM_DISTANCE_METERS, mSkyLocationListener);
  168. }
  169.  
  170. Toolbar toolbar = findViewById(R.id.toolbar);
  171. setSupportActionBar(toolbar);
  172.  
  173. mDrawerLayout = findViewById(R.id.drawer_layout);
  174. ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
  175. this, mDrawerLayout, toolbar, R.string.navigation_open, R.string.navigation_close);
  176. mDrawerLayout.addDrawerListener(toggle);
  177. toggle.syncState();
  178.  
  179. setTransactionToQoSOptionsListener();
  180.  
  181. setDataRelatedToLoggedInUser(toolbar);
  182.  
  183. prepareItemsInMenu();
  184.  
  185. // Disable the navigation when we display fragments that appear before the IPTVFragment.
  186. enableNavigationItems(false);
  187. // When user log in, first fragment that appear is PickDestinationFragment.
  188. proceedToFragment(mPickDestinationFragment);
  189. }
  190.  
  191. private void prepareNavigationView() {
  192. navigationView.getMenu().clear();
  193. if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  194. navigationView.inflateMenu(R.menu.navigation_drawer_menu);
  195. navigationView.setBackgroundColor(Color.WHITE);
  196. mListView.setVisibility(View.GONE);
  197. headerLayout.setVisibility(View.VISIBLE);
  198. } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  199. navigationView.setBackgroundColor(Color.BLACK);
  200. mListView.setVisibility(View.VISIBLE);
  201. headerLayout.setVisibility(View.GONE);
  202. }
  203. }
  204.  
  205. @Override
  206. protected void onDestroy() {
  207. MediaPlayerController.getInstance().deinit();
  208. if (mLocationManager != null && mSkyLocationListener != null) {
  209. mLocationManager.removeUpdates(mSkyLocationListener);
  210. mSkyLocationListener = null;
  211. }
  212.  
  213. super.onDestroy();
  214. }
  215.  
  216. /**
  217. * This method implements the behavior of the transition between the fragments,
  218. * by implementing each of the fragment callbacks.
  219. */
  220. private void prepareFragments() {
  221.  
  222. mRouteDisplayCallback = new IFragmentCallback() {
  223. @Override
  224. public void onSendRequest(FragmentMessage message, Object... args) {
  225. if (message.equals(FragmentMessage.PROCEED)) {
  226. QoSOptionsFragment qoSOptionsFragment = QoSOptionsFragment.newInstance((int) args[0]);
  227. qoSOptionsFragment.setFragmentCallback(mQosOptionsCallback);
  228. proceedToFragment(qoSOptionsFragment);
  229. }
  230. }
  231. };
  232.  
  233. mQosOptionsCallback = new IFragmentCallback() {
  234. /**
  235. * Used to indicate if a user is entering this fragment for the first time.
  236. */
  237. private boolean firstFragmentEntry = true;
  238.  
  239. @Override
  240. public void onSendRequest(FragmentMessage message, Object... args) {
  241. switch (message) {
  242. case CONFIRM:
  243. int delayMinutes = (int) args[0];
  244. boolean playPrefetchedContent = (boolean) args[1];
  245. boolean suggestStopovers = (boolean) args[2];
  246. AppUser loggedInUser = AppUser.fromJsonString(Utils.getSharedPreferences(getApplicationContext()).getString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, null));
  247.  
  248. if (loggedInUser != null) {
  249. loggedInUser.setDelay(delayMinutes);
  250. loggedInUser.setPrefetchedContent(playPrefetchedContent);
  251. loggedInUser.setSuggestingStopovers(suggestStopovers);
  252. mDatabaseController.updateUser(loggedInUser);
  253. Utils.getSharedPreferences(getApplicationContext()).edit().putString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, loggedInUser.toString()).apply();
  254. }
  255.  
  256. if (DEBUG) {
  257. Log.d(TAG, "[QosFragment][FragmentCallback][buffer length: " +
  258. args[0] + " , play prefetched content: " + playPrefetchedContent +
  259. " , suggest stopovers: " + suggestStopovers + "]");
  260. }
  261.  
  262. //TODO: Check if last configured buffer length is different than this.
  263. EventQueue.getInstance().dispatch(new Event(EventType.EV_PLAYER_SET_BUFFER_LENGTH,
  264. // Convert minutes to milliseconds.
  265. (delayMinutes * 60 * 1000)));
  266. EventQueue.getInstance().dispatch(new Event(EventType.EV_CHANGE_TV_CHANNEL,
  267. MediaPlayerController.getInstance().getTVControls().getCurrentChannel().getURL()));
  268. break;
  269. case SKIP:
  270. if (firstFragmentEntry) {
  271. if (DEBUG) {
  272. Log.d(TAG, "[QosFragment][FragmentCallback][set default buffer length, " +
  273. "entered screen first time after log in]");
  274. }
  275. // Create the default player, if user clicks SKIP for the first time.
  276. EventQueue.getInstance().dispatch(new Event(EventType.EV_PLAYER_SET_BUFFER_LENGTH,
  277. Constants.SkyPlayerConstants.DEFAULT_BUFFER_LENGTH_MILLIS));
  278. EventQueue.getInstance().dispatch(new Event(EventType.EV_CHANGE_TV_CHANNEL,
  279. MediaPlayerController.getInstance().getTVControls().getCurrentChannel().getURL()));
  280. }
  281. default:
  282. break;
  283. }
  284. firstFragmentEntry = false;
  285. initialFragment(mIpTVFragment);
  286. // Enable navigation when we display TV.
  287. enableNavigationItems(true);
  288. }
  289. };
  290.  
  291. mPickDestinationFragment.setFragmentCallback(new IFragmentCallback() {
  292. @Override
  293. public void onSendRequest(FragmentMessage message, Object... args) {
  294. switch (message) {
  295. case CONFIRM:
  296. if (args.length == 2) {
  297. RouteInfo routeInfo = (RouteInfo) args[0];
  298. // Set new strategy when we change trajectory.
  299. ParameterQueue.getInstance().dispatch(new QueueElement(Parameter.TRAJECTORY.toString(), routeInfo));
  300. MediaPlayerController.getInstance().setStrategy(new BufferStrategy());
  301. RouteDisplayFragment routeDisplayFragment = RouteDisplayFragment.newInstance((RouteInfo) args[0],
  302. (Place) args[1]);
  303. /**
  304. * loggedInUser used to update database to preferences
  305. */
  306. AppUser loggedInUser = AppUser.fromJsonString(Utils.getSharedPreferences(getApplicationContext()).getString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, null));
  307.  
  308. if (loggedInUser != null) {
  309. loggedInUser.setDestination(String.valueOf(((Place) args[1]).getName()));
  310. mDatabaseController.updateUser(loggedInUser);
  311. Utils.getSharedPreferences(getApplicationContext()).edit().putString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, loggedInUser.toString()).apply();
  312. }
  313. routeDisplayFragment.setFragmentCallback(mRouteDisplayCallback);
  314. proceedToFragment(routeDisplayFragment);
  315. }
  316. break;
  317. case SKIP:
  318. QoSOptionsFragment qoSOptionsFragment = QoSOptionsFragment.newInstance(
  319. Constants.SkyPlayerConstants.DEFAULT_BUFFER_LENGTH_MINUTES);
  320. qoSOptionsFragment.setFragmentCallback(mQosOptionsCallback);
  321. proceedToFragment(qoSOptionsFragment);
  322. break;
  323. default:
  324. break;
  325. }
  326. }
  327. });
  328. }
  329.  
  330. /**
  331. * Changes current screen output(fragment).
  332. *
  333. * @param fragment - screen that will be displayed
  334. */
  335. private void proceedToFragment(Fragment fragment) {
  336. getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment).addToBackStack(null).commit();
  337. }
  338.  
  339. private void initialFragment(Fragment fragment) {
  340. getSupportFragmentManager().beginTransaction().add(R.id.frame, fragment).commit();
  341. }
  342.  
  343. /**
  344. * Used to enable/disable navigation(top bar + sidebar).
  345. *
  346. * @param enable if this value is true, it will enable navigation, otherwise disable.
  347. */
  348. private void enableNavigationItems(boolean enable) {
  349. if (enable) {
  350. getSupportActionBar().show();
  351. mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
  352. } else {
  353. getSupportActionBar().hide();
  354. mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
  355. }
  356. }
  357.  
  358. /**
  359. * Applying font type to items in menu
  360. * @param mi - current menu item
  361. */
  362. private void applyFontToMenuItem(MenuItem mi) {
  363. Typeface font = Typeface.createFromAsset(getAssets(), Constants.HomeScreenConstants.APP_FONT);
  364. SpannableString mNewTitle = new SpannableString(mi.getTitle());
  365. mNewTitle.setSpan(new CustomTypeFont("", font), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
  366. mi.setTitle(mNewTitle);
  367. }
  368.  
  369. /**
  370. * Setting listener on items in list view
  371. */
  372. private void setChannelListOnClick(){
  373. mTVControls = MediaPlayerController.getInstance().getTVControls();
  374.  
  375. mListView = findViewById(R.id.channel_list);
  376.  
  377. TVChannelArrayAdapter adapter = new TVChannelArrayAdapter(this, R.layout.simple_list_item, mTVControls.getChannelList());
  378. mListView.setAdapter(adapter);
  379.  
  380. mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  381. @Override
  382. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  383. try {
  384. mTVControls.changeChannel(mTVControls.getChannelList().get(position));
  385. Log.d(TAG, "[onItemClick][" + mTVControls.getChannelList().get(position) + "]");
  386. } catch (IndexOutOfBoundsException e) {
  387. Log.e(TAG, "[onItemClick][" + e + "]");
  388. }
  389. }
  390. });
  391. }
  392.  
  393. /**
  394. * Setting listener on settings button, in dependency of orientation (because in landscape mode is not possible to go to options fragments,
  395. * only switching channels is available)
  396. */
  397. private void setTransactionToQoSOptionsListener(){
  398. NavigationView navigationView = findViewById(R.id.nav_view);
  399.  
  400. prepareNavigationView();
  401. if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
  402. navigationView.setNavigationItemSelectedListener(new NavigationController(this, mIpTVFragment, mPickDestinationFragment));
  403. } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
  404. navigationView.setNavigationItemSelectedListener(null);
  405. }
  406. }
  407.  
  408. /**
  409. * Set design of header layout related to logged in user
  410. * @param toolbar - header layout
  411. */
  412. private void setDataRelatedToLoggedInUser(Toolbar toolbar){
  413. AppUser loggedInUser = AppUser.fromJsonString(Utils.getSharedPreferences(this).getString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, null));
  414. if (loggedInUser != null) {
  415. TextView textViewUsername = navHeader.findViewById(R.id.username);
  416. textViewUsername.setText(loggedInUser.getUsername());
  417.  
  418. RelativeLayout headerLayout = navHeader.findViewById(R.id.headers_layout);
  419. try {
  420. headerLayout.setBackgroundColor(Integer.parseInt(loggedInUser.getApplicationUIDesign()));
  421. toolbar.setBackgroundColor(Integer.parseInt(loggedInUser.getApplicationUIDesign()));
  422. } catch (Exception e) {
  423.  
  424. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  425. headerLayout.setBackgroundColor(getColor(R.color.colorPrimary));
  426. toolbar.setBackgroundColor(getColor(R.color.colorPrimary));
  427. } else {
  428. headerLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
  429. toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
  430. }
  431. }
  432. } else {
  433. finish();
  434. startActivity(new Intent(this, LoginActivity.class));
  435. }
  436. }
  437.  
  438. /**
  439. * Setting font to all items in menu
  440. */
  441. private void prepareItemsInMenu(){
  442. Menu m = navigationView.getMenu();
  443. for (int i = 0; i < m.size(); i++) {
  444. MenuItem mi = m.getItem(i);
  445.  
  446. //for applying a font to subMenu ...
  447. SubMenu subMenu = mi.getSubMenu();
  448. if (subMenu != null && subMenu.size() > 0) {
  449. for (int j = 0; j < subMenu.size(); j++) {
  450. MenuItem subMenuItem = subMenu.getItem(j);
  451. applyFontToMenuItem(subMenuItem);
  452. }
  453. }
  454.  
  455. //the method we have create in activity
  456. applyFontToMenuItem(mi);
  457. }
  458. }
  459.  
  460. @Override
  461. public void onBackPressed() {
  462. Fragment currentDisplayFragment = getSupportFragmentManager().findFragmentById(R.id.frame);
  463. if (DEBUG) {
  464. Log.d(TAG, "[onBackPressed][" + currentDisplayFragment.getClass().getSimpleName() + "]");
  465. }
  466. LayoutInflater layoutInflater = LayoutInflater.from(this);
  467. View view = layoutInflater.inflate(R.layout.exit_dialog_layout,null);
  468.  
  469. if (currentDisplayFragment == mPickDestinationFragment ||
  470. currentDisplayFragment == mIpTVFragment) {
  471. new AlertDialog.Builder(this)
  472. .setTitle("Are you sure you want to exit?")
  473. .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
  474. @Override
  475. public void onClick(DialogInterface dialog, int which) {
  476. finish();
  477. }
  478. })
  479. .setNegativeButton("No", null)
  480. .show();
  481. } else {
  482. super.onBackPressed();
  483. }
  484.  
  485. }
  486.  
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement