Replaced ActionBarSherlock with Support Library appcompat.

This commit is contained in:
Felix Ableitner 2013-09-24 01:45:04 +02:00
parent 3207eb3e5b
commit fa6d444d1b
6 changed files with 162 additions and 159 deletions

View file

@ -17,7 +17,7 @@
android:allowBackup="true" android:allowBackup="true"
android:icon="@drawable/ic_launcher" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light" > android:theme="@style/Theme.AppCompat.Light" >
<activity <activity
android:name="com.github.nutomic.controldlna.gui.MainActivity" android:name="com.github.nutomic.controldlna.gui.MainActivity"

Binary file not shown.

View file

@ -12,4 +12,4 @@
# Project target. # Project target.
target=android-18 target=android-18
android.library.reference.1=../actionbarsherlock android.library.reference.1=../android-support-v7-appcompat

View file

@ -1,6 +1,6 @@
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android" <?xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools" <android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager" android:id="@+id/pager"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent" />
tools:context=".MainActivity" />

View file

@ -28,184 +28,145 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.github.nutomic.controldlna.gui; package com.github.nutomic.controldlna.gui;
import android.os.Bundle; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction; import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager; import android.support.v4.view.ViewPager;
import android.util.Log; import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBar.Tab;
import android.support.v7.app.ActionBar.TabListener;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent; import android.view.KeyEvent;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.github.nutomic.controldlna.R; import com.github.nutomic.controldlna.R;
import com.github.nutomic.controldlna.upnp.UpnpPlayer; import com.github.nutomic.controldlna.upnp.UpnpPlayer;
/** /**
* Main activity, with tabs for media servers and media renderers. * Main activity, with tabs for media servers and media renderers.
* *
* @author Felix Ableitner * @author Felix Ableitner
* *
*/ */
public class MainActivity extends SherlockFragmentActivity implements public class MainActivity extends ActionBarActivity {
ActionBar.TabListener {
/**
* Interface which allows listening to "back" button presses.
*/
public interface OnBackPressedListener {
boolean onBackPressed();
}
/** /**
* Manages all UPNP connections including playback. * Manages all UPNP connections including playback.
*/ */
private UpnpPlayer mPlayer = new UpnpPlayer(); private UpnpPlayer mPlayer = new UpnpPlayer();
/**
* Provides Fragments, holding all of them in memory.
*/
private SectionsPagerAdapter mSectionsPagerAdapter =
new SectionsPagerAdapter(getSupportFragmentManager());
/**
* Holds the section contents.
*/
private ViewPager mViewPager;
/** /**
* Fragment for first tab, holding media renderers. * Holds fragments.
*/ */
private RendererFragment mRendererFragment = new RendererFragment(); SectionsPagerAdapter mSectionsPagerAdapter;
/** /**
* Fragment for second tab, holding media servers. * Allows tab swiping.
*/ */
private ServerFragment mServerFragment = new ServerFragment(); ViewPager mViewPager;
@Override public void onCreate(Bundle savedInstanceState) {
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState); final ActionBar actionBar = getSupportActionBar();
setContentView(R.layout.activity_main);
// Set up the action bar. actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
final ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false); setContentView(R.layout.activity_main);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mViewPager = (ViewPager) findViewById(R.id.pager); // ViewPager and its adapters use support library
mViewPager.setAdapter(mSectionsPagerAdapter); // fragments, so use getSupportFragmentManager.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOnPageChangeListener(
new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// Select correct tab after swiping. TabListener tabListener = new ActionBar.TabListener() {
mViewPager.setOnPageChangeListener( public void onTabSelected(ActionBar.Tab tab, FragmentTransaction ft) {
new ViewPager.SimpleOnPageChangeListener() { mViewPager.setCurrentItem(tab.getPosition());
@Override }
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
actionBar.addTab(actionBar.newTab() @Override
.setText(R.string.title_server) public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
.setTabListener(this));
actionBar.addTab(actionBar.newTab()
.setText(R.string.title_renderer)
.setTabListener(this));
mPlayer.open(getApplicationContext());
}
@Override
protected void onDestroy() {
super.onDestroy();
mPlayer.close(getApplicationContext());
}
@Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
mViewPager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
@Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
/**
* Returns Fragment corresponding to current tab.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return mServerFragment;
case 1: return mRendererFragment;
default: return null;
} }
}
@Override @Override
public int getCount() { public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
return 2; }
} };
}
actionBar.addTab(actionBar.newTab()
/** .setText(R.string.title_server)
* Listener for the 'back' key. .setTabListener(tabListener));
*/ actionBar.addTab(actionBar.newTab()
public interface OnBackPressedListener { .setText(R.string.title_renderer)
public boolean onBackPressed(); .setTabListener(tabListener));
}
mPlayer.open(getApplicationContext());
/**
* Forwards back press to active Fragment (unless the fragment is
* showing its root view).
*/
@Override
public void onBackPressed() {
OnBackPressedListener currentFragment = (OnBackPressedListener)
mSectionsPagerAdapter.getItem(mViewPager.getCurrentItem());
if (!currentFragment.onBackPressed())
super.onBackPressed();
}
/**
* Changes volume on key press.
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (event.getAction() == KeyEvent.ACTION_DOWN)
mPlayer.increaseVolume();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (event.getAction() == KeyEvent.ACTION_DOWN)
mPlayer.decreaseVolume();
return true;
default:
return super.dispatchKeyEvent(event);
}
} }
/** @Override
* Returns shared instance of UpnpPlayer. protected void onDestroy() {
*/ super.onDestroy();
public UpnpPlayer getUpnpPlayer() { mPlayer.close(getApplicationContext());
}
/**
* Forwards back press to active Fragment (unless the fragment is
* showing its root view).
*/
@Override
public void onBackPressed() {
OnBackPressedListener currentFragment = (OnBackPressedListener)
mSectionsPagerAdapter.getItem(mViewPager.getCurrentItem());
if (!currentFragment.onBackPressed())
super.onBackPressed();
}
/**
* Changes volume on key press.
*/
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_VOLUME_UP:
if (event.getAction() == KeyEvent.ACTION_DOWN)
mPlayer.increaseVolume();
return true;
case KeyEvent.KEYCODE_VOLUME_DOWN:
if (event.getAction() == KeyEvent.ACTION_DOWN)
mPlayer.decreaseVolume();
return true;
default:
return super.dispatchKeyEvent(event);
}
}
/**
* Returns shared instance of UPNP player.
* @return
*/
public UpnpPlayer getUpnpPlayer() {
return mPlayer; return mPlayer;
}
}
/**
* Opens the UPNP renderer tab in the activity.
*/
public void switchToRendererTab() {
Log.d("tag", "called");
getSupportActionBar().selectTab(getSupportActionBar().getTabAt(1));
}
/**
* Switches to the "renderer" tab.
*/
public void switchToRendererTab() {
mViewPager.setCurrentItem(1);
}
} }

View file

@ -0,0 +1,42 @@
package com.github.nutomic.controldlna.gui;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
//Since this is an object collection, use a FragmentStatePagerAdapter,
//and NOT a FragmentPagerAdapter.
public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
/**
* Fragment for second tab, holding media servers.
*/
private ServerFragment mServerFragment = new ServerFragment();
/**
* Fragment for first tab, holding media renderers.
*/
private RendererFragment mRendererFragment = new RendererFragment();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0: return mServerFragment;
case 1: return mRendererFragment;
default: return null;
}
}
@Override
public int getCount() {
return 2;
}
}