Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ************************************************************************
- * <p/>
- * <p/>
- * Copyright (c) 2019 RT-RK
- * Bulevar Narodnog Fronta 23a
- * <p/>
- * All Rights Reserved
- * <p/>
- * P R O P R I E T A R Y & C O N F I D E N T I A L
- * <p/>
- * -----------------------------------------------------
- * http://www.rt-rk.com
- * -----------------------------------------------------
- * <p/>
- * <p/>
- * <p/>
- *
- * @author Skywalker
- * <p/>
- * <p/>
- * ************************************************************************
- */
- package com.rtrk.automotive.skywalker.skyplayer.app.home;
- import android.annotation.SuppressLint;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.content.res.Configuration;
- import android.graphics.Color;
- import android.graphics.Typeface;
- import android.location.LocationListener;
- import android.location.LocationManager;
- import android.os.Build;
- import android.os.Bundle;
- import android.text.Spannable;
- import android.text.SpannableString;
- import android.util.Log;
- import android.util.TypedValue;
- import android.view.Gravity;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.SubMenu;
- import android.view.View;
- import android.view.WindowManager;
- import android.widget.AdapterView;
- import android.widget.ListView;
- import android.widget.RelativeLayout;
- import android.widget.TextView;
- import androidx.appcompat.app.ActionBarDrawerToggle;
- import androidx.appcompat.app.AppCompatActivity;
- import androidx.appcompat.widget.Toolbar;
- import androidx.drawerlayout.widget.DrawerLayout;
- import androidx.fragment.app.Fragment;
- import com.google.android.libraries.places.api.model.Place;
- import com.google.android.material.navigation.NavigationView;
- import com.rtrk.automotive.skywalker.skyplayer.R;
- import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.FragmentMessage;
- import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.IFragmentCallback;
- import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.IPTVFragment;
- import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.PickDestinationFragment;
- import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.QoSOptionsFragment;
- import com.rtrk.automotive.skywalker.skyplayer.app.home.fragments.RouteDisplayFragment;
- import com.rtrk.automotive.skywalker.skyplayer.app.login.LoginActivity;
- import com.rtrk.automotive.skywalker.skyplayer.player.MediaPlayerController;
- import com.rtrk.automotive.skywalker.skyplayer.player.tv.TVControls;
- import com.rtrk.automotive.skywalker.skyplayer.server.DatabaseController;
- import com.rtrk.automotive.skywalker.skyplayer.server.RouteInfo;
- import com.rtrk.automotive.skywalker.skyplayer.server.SkyLocationListener;
- import com.rtrk.automotive.skywalker.skyplayer.server.model.AppUser;
- import com.rtrk.automotive.skywalker.skyplayer.strategies.BufferStrategy;
- import com.rtrk.automotive.skywalker.skyplayer.strategies.model.Event;
- import com.rtrk.automotive.skywalker.skyplayer.strategies.model.EventQueue;
- import com.rtrk.automotive.skywalker.skyplayer.strategies.model.EventType;
- import com.rtrk.automotive.skywalker.skyplayer.strategies.model.Parameter;
- import com.rtrk.automotive.skywalker.skyplayer.strategies.model.ParameterQueue;
- import com.rtrk.automotive.skywalker.skyplayer.strategies.model.QueueElement;
- import com.rtrk.automotive.skywalker.skyplayer.utils.Constants;
- import com.rtrk.automotive.skywalker.skyplayer.utils.CustomTypeFont;
- import com.rtrk.automotive.skywalker.skyplayer.utils.Utils;
- import java.io.File;
- import java.net.URISyntaxException;
- public class HomeActivity extends AppCompatActivity {
- private static final boolean DEBUG = Constants.Debug.DEBUG_HOME;
- private static final String TAG = HomeActivity.class.getSimpleName();
- //TODO: Make instance of data collection controller when implementation is done
- private IPTVFragment mIpTVFragment;
- private PickDestinationFragment mPickDestinationFragment;
- private IFragmentCallback mRouteDisplayCallback;
- private IFragmentCallback mQosOptionsCallback;
- private LocationListener mSkyLocationListener = null;
- private LocationManager mLocationManager = null;
- /**
- * Used to hide/show sidebar that's part of the navigation bar.
- */
- private DrawerLayout mDrawerLayout;
- private NavigationView navigationView;
- private View navHeader;
- private RelativeLayout headerLayout;
- /**
- * ListView used to display channel list
- */
- private ListView mListView;
- /**
- * {@link TVControls} used for channel switching
- */
- private TVControls mTVControls;
- /**
- * Controller for database used in application
- */
- private DatabaseController mDatabaseController;
- @Override
- public void onConfigurationChanged(Configuration newConfig) {
- super.onConfigurationChanged(newConfig);
- //Remove all existing items from the menu, leaving it empty as if it had just been created.
- prepareNavigationView();
- if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- getSupportActionBar().show();
- } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
- getSupportActionBar().hide();
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
- }
- }
- @SuppressLint("MissingPermission")
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_home);
- mSkyLocationListener = new SkyLocationListener(getApplicationContext());
- MediaPlayerController.getInstance().init(this);
- MediaPlayerController.getInstance().setStrategy(new BufferStrategy());
- mDatabaseController = new DatabaseController(this);
- navigationView = findViewById(R.id.nav_view);
- navHeader = navigationView.getHeaderView(Constants.HomeScreenConstants.HOME_NAVIGATION_VIEW_DEFAULT_POSITION);
- headerLayout = navHeader.findViewById(R.id.headers_layout);
- mPickDestinationFragment = new PickDestinationFragment();
- mIpTVFragment = new IPTVFragment();
- setChannelListOnClick();
- prepareFragments();
- /**
- * Location manager that listens to location change
- */
- mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
- if (mLocationManager != null) {
- mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, Constants.SkyLocationListenerConstants.MINIMUM_TIME_INTERVAL_MILLIS, Constants.SkyLocationListenerConstants.MINIMUM_DISTANCE_METERS, mSkyLocationListener);
- }
- Toolbar toolbar = findViewById(R.id.toolbar);
- setSupportActionBar(toolbar);
- mDrawerLayout = findViewById(R.id.drawer_layout);
- ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
- this, mDrawerLayout, toolbar, R.string.navigation_open, R.string.navigation_close);
- mDrawerLayout.addDrawerListener(toggle);
- toggle.syncState();
- setTransactionToQoSOptionsListener();
- setDataRelatedToLoggedInUser(toolbar);
- prepareItemsInMenu();
- // Disable the navigation when we display fragments that appear before the IPTVFragment.
- enableNavigationItems(false);
- // When user log in, first fragment that appear is PickDestinationFragment.
- proceedToFragment(mPickDestinationFragment);
- }
- private void prepareNavigationView() {
- navigationView.getMenu().clear();
- if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- navigationView.inflateMenu(R.menu.navigation_drawer_menu);
- navigationView.setBackgroundColor(Color.WHITE);
- mListView.setVisibility(View.GONE);
- headerLayout.setVisibility(View.VISIBLE);
- } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
- navigationView.setBackgroundColor(Color.BLACK);
- mListView.setVisibility(View.VISIBLE);
- headerLayout.setVisibility(View.GONE);
- }
- }
- @Override
- protected void onDestroy() {
- MediaPlayerController.getInstance().deinit();
- if (mLocationManager != null && mSkyLocationListener != null) {
- mLocationManager.removeUpdates(mSkyLocationListener);
- mSkyLocationListener = null;
- }
- super.onDestroy();
- }
- /**
- * This method implements the behavior of the transition between the fragments,
- * by implementing each of the fragment callbacks.
- */
- private void prepareFragments() {
- mRouteDisplayCallback = new IFragmentCallback() {
- @Override
- public void onSendRequest(FragmentMessage message, Object... args) {
- if (message.equals(FragmentMessage.PROCEED)) {
- QoSOptionsFragment qoSOptionsFragment = QoSOptionsFragment.newInstance((int) args[0]);
- qoSOptionsFragment.setFragmentCallback(mQosOptionsCallback);
- proceedToFragment(qoSOptionsFragment);
- }
- }
- };
- mQosOptionsCallback = new IFragmentCallback() {
- /**
- * Used to indicate if a user is entering this fragment for the first time.
- */
- private boolean firstFragmentEntry = true;
- @Override
- public void onSendRequest(FragmentMessage message, Object... args) {
- switch (message) {
- case CONFIRM:
- int delayMinutes = (int) args[0];
- boolean playPrefetchedContent = (boolean) args[1];
- boolean suggestStopovers = (boolean) args[2];
- AppUser loggedInUser = AppUser.fromJsonString(Utils.getSharedPreferences(getApplicationContext()).getString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, null));
- if (loggedInUser != null) {
- loggedInUser.setDelay(delayMinutes);
- loggedInUser.setPrefetchedContent(playPrefetchedContent);
- loggedInUser.setSuggestingStopovers(suggestStopovers);
- mDatabaseController.updateUser(loggedInUser);
- Utils.getSharedPreferences(getApplicationContext()).edit().putString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, loggedInUser.toString()).apply();
- }
- if (DEBUG) {
- Log.d(TAG, "[QosFragment][FragmentCallback][buffer length: " +
- args[0] + " , play prefetched content: " + playPrefetchedContent +
- " , suggest stopovers: " + suggestStopovers + "]");
- }
- //TODO: Check if last configured buffer length is different than this.
- EventQueue.getInstance().dispatch(new Event(EventType.EV_PLAYER_SET_BUFFER_LENGTH,
- // Convert minutes to milliseconds.
- (delayMinutes * 60 * 1000)));
- EventQueue.getInstance().dispatch(new Event(EventType.EV_CHANGE_TV_CHANNEL,
- MediaPlayerController.getInstance().getTVControls().getCurrentChannel().getURL()));
- break;
- case SKIP:
- if (firstFragmentEntry) {
- if (DEBUG) {
- Log.d(TAG, "[QosFragment][FragmentCallback][set default buffer length, " +
- "entered screen first time after log in]");
- }
- // Create the default player, if user clicks SKIP for the first time.
- EventQueue.getInstance().dispatch(new Event(EventType.EV_PLAYER_SET_BUFFER_LENGTH,
- Constants.SkyPlayerConstants.DEFAULT_BUFFER_LENGTH_MILLIS));
- EventQueue.getInstance().dispatch(new Event(EventType.EV_CHANGE_TV_CHANNEL,
- MediaPlayerController.getInstance().getTVControls().getCurrentChannel().getURL()));
- }
- default:
- break;
- }
- firstFragmentEntry = false;
- initialFragment(mIpTVFragment);
- // Enable navigation when we display TV.
- enableNavigationItems(true);
- }
- };
- mPickDestinationFragment.setFragmentCallback(new IFragmentCallback() {
- @Override
- public void onSendRequest(FragmentMessage message, Object... args) {
- switch (message) {
- case CONFIRM:
- if (args.length == 2) {
- RouteInfo routeInfo = (RouteInfo) args[0];
- // Set new strategy when we change trajectory.
- ParameterQueue.getInstance().dispatch(new QueueElement(Parameter.TRAJECTORY.toString(), routeInfo));
- MediaPlayerController.getInstance().setStrategy(new BufferStrategy());
- RouteDisplayFragment routeDisplayFragment = RouteDisplayFragment.newInstance((RouteInfo) args[0],
- (Place) args[1]);
- /**
- * loggedInUser used to update database to preferences
- */
- AppUser loggedInUser = AppUser.fromJsonString(Utils.getSharedPreferences(getApplicationContext()).getString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, null));
- if (loggedInUser != null) {
- loggedInUser.setDestination(String.valueOf(((Place) args[1]).getName()));
- mDatabaseController.updateUser(loggedInUser);
- Utils.getSharedPreferences(getApplicationContext()).edit().putString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, loggedInUser.toString()).apply();
- }
- routeDisplayFragment.setFragmentCallback(mRouteDisplayCallback);
- proceedToFragment(routeDisplayFragment);
- }
- break;
- case SKIP:
- QoSOptionsFragment qoSOptionsFragment = QoSOptionsFragment.newInstance(
- Constants.SkyPlayerConstants.DEFAULT_BUFFER_LENGTH_MINUTES);
- qoSOptionsFragment.setFragmentCallback(mQosOptionsCallback);
- proceedToFragment(qoSOptionsFragment);
- break;
- default:
- break;
- }
- }
- });
- }
- /**
- * Changes current screen output(fragment).
- *
- * @param fragment - screen that will be displayed
- */
- private void proceedToFragment(Fragment fragment) {
- getSupportFragmentManager().beginTransaction().replace(R.id.frame, fragment).addToBackStack(null).commit();
- }
- private void initialFragment(Fragment fragment) {
- getSupportFragmentManager().beginTransaction().add(R.id.frame, fragment).commit();
- }
- /**
- * Used to enable/disable navigation(top bar + sidebar).
- *
- * @param enable if this value is true, it will enable navigation, otherwise disable.
- */
- private void enableNavigationItems(boolean enable) {
- if (enable) {
- getSupportActionBar().show();
- mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
- } else {
- getSupportActionBar().hide();
- mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
- }
- }
- /**
- * Applying font type to items in menu
- * @param mi - current menu item
- */
- private void applyFontToMenuItem(MenuItem mi) {
- Typeface font = Typeface.createFromAsset(getAssets(), Constants.HomeScreenConstants.APP_FONT);
- SpannableString mNewTitle = new SpannableString(mi.getTitle());
- mNewTitle.setSpan(new CustomTypeFont("", font), 0, mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
- mi.setTitle(mNewTitle);
- }
- /**
- * Setting listener on items in list view
- */
- private void setChannelListOnClick(){
- mTVControls = MediaPlayerController.getInstance().getTVControls();
- mListView = findViewById(R.id.channel_list);
- TVChannelArrayAdapter adapter = new TVChannelArrayAdapter(this, R.layout.simple_list_item, mTVControls.getChannelList());
- mListView.setAdapter(adapter);
- mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
- try {
- mTVControls.changeChannel(mTVControls.getChannelList().get(position));
- Log.d(TAG, "[onItemClick][" + mTVControls.getChannelList().get(position) + "]");
- } catch (IndexOutOfBoundsException e) {
- Log.e(TAG, "[onItemClick][" + e + "]");
- }
- }
- });
- }
- /**
- * Setting listener on settings button, in dependency of orientation (because in landscape mode is not possible to go to options fragments,
- * only switching channels is available)
- */
- private void setTransactionToQoSOptionsListener(){
- NavigationView navigationView = findViewById(R.id.nav_view);
- prepareNavigationView();
- if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
- navigationView.setNavigationItemSelectedListener(new NavigationController(this, mIpTVFragment, mPickDestinationFragment));
- } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
- navigationView.setNavigationItemSelectedListener(null);
- }
- }
- /**
- * Set design of header layout related to logged in user
- * @param toolbar - header layout
- */
- private void setDataRelatedToLoggedInUser(Toolbar toolbar){
- AppUser loggedInUser = AppUser.fromJsonString(Utils.getSharedPreferences(this).getString(Constants.SharedPreferencesConstants.PREF_LOGGED_IN_USER, null));
- if (loggedInUser != null) {
- TextView textViewUsername = navHeader.findViewById(R.id.username);
- textViewUsername.setText(loggedInUser.getUsername());
- RelativeLayout headerLayout = navHeader.findViewById(R.id.headers_layout);
- try {
- headerLayout.setBackgroundColor(Integer.parseInt(loggedInUser.getApplicationUIDesign()));
- toolbar.setBackgroundColor(Integer.parseInt(loggedInUser.getApplicationUIDesign()));
- } catch (Exception e) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- headerLayout.setBackgroundColor(getColor(R.color.colorPrimary));
- toolbar.setBackgroundColor(getColor(R.color.colorPrimary));
- } else {
- headerLayout.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
- toolbar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
- }
- }
- } else {
- finish();
- startActivity(new Intent(this, LoginActivity.class));
- }
- }
- /**
- * Setting font to all items in menu
- */
- private void prepareItemsInMenu(){
- Menu m = navigationView.getMenu();
- for (int i = 0; i < m.size(); i++) {
- MenuItem mi = m.getItem(i);
- //for applying a font to subMenu ...
- SubMenu subMenu = mi.getSubMenu();
- if (subMenu != null && subMenu.size() > 0) {
- for (int j = 0; j < subMenu.size(); j++) {
- MenuItem subMenuItem = subMenu.getItem(j);
- applyFontToMenuItem(subMenuItem);
- }
- }
- //the method we have create in activity
- applyFontToMenuItem(mi);
- }
- }
- @Override
- public void onBackPressed() {
- Fragment currentDisplayFragment = getSupportFragmentManager().findFragmentById(R.id.frame);
- if (DEBUG) {
- Log.d(TAG, "[onBackPressed][" + currentDisplayFragment.getClass().getSimpleName() + "]");
- }
- LayoutInflater layoutInflater = LayoutInflater.from(this);
- View view = layoutInflater.inflate(R.layout.exit_dialog_layout,null);
- if (currentDisplayFragment == mPickDestinationFragment ||
- currentDisplayFragment == mIpTVFragment) {
- new AlertDialog.Builder(this)
- .setTitle("Are you sure you want to exit?")
- .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- finish();
- }
- })
- .setNegativeButton("No", null)
- .show();
- } else {
- super.onBackPressed();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement