Java tutorial
/* * Copyright (C) 2012 Andrew Neal * Copyright (C) 2014 The CyanogenMod Project * Copyright (C) 2015 Naman Dwivedi * * Licensed under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with the * License. You may obtain a copy of the License at * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law * or agreed to in writing, software distributed under the License is * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ package com.musiqueplayer.playlistequalizerandroidwear.activities; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.media.AudioManager; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.IBinder; import android.support.annotation.Nullable; import android.support.v4.app.FragmentManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Toast; import com.afollestad.appthemeengine.ATE; import com.afollestad.appthemeengine.ATEActivity; import com.musiqueplayer.playlistequalizerandroidwear.ITimberService; import com.musiqueplayer.playlistequalizerandroidwear.MusicPlayer; import com.musiqueplayer.playlistequalizerandroidwear.MusicService; import com.musiqueplayer.playlistequalizerandroidwear.R; import com.musiqueplayer.playlistequalizerandroidwear.listeners.MusicStateListener; import com.musiqueplayer.playlistequalizerandroidwear.slidinguppanel.SlidingUpPanelLayout; import com.musiqueplayer.playlistequalizerandroidwear.subfragments.QuickControlsFragment; import com.musiqueplayer.playlistequalizerandroidwear.utils.Helpers; import com.musiqueplayer.playlistequalizerandroidwear.utils.NavigationUtils; import com.musiqueplayer.playlistequalizerandroidwear.utils.TimberUtils; import java.lang.ref.WeakReference; import java.util.ArrayList; import static com.musiqueplayer.playlistequalizerandroidwear.MusicPlayer.mService; public class BaseActivity extends ATEActivity implements ServiceConnection, MusicStateListener { private final ArrayList<MusicStateListener> mMusicStateListener = new ArrayList<>(); private MusicPlayer.ServiceToken mToken; private PlaybackStatus mPlaybackStatus; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mToken = MusicPlayer.bindToService(this, this); AppRater.app_launched(this); mPlaybackStatus = new PlaybackStatus(this); //make volume keys change multimedia volume even if music is not playing now setVolumeControlStream(AudioManager.STREAM_MUSIC); } @Override protected void onStart() { super.onStart(); final IntentFilter filter = new IntentFilter(); // Play and pause changes filter.addAction(MusicService.PLAYSTATE_CHANGED); // Track changes filter.addAction(MusicService.META_CHANGED); // Update a list, probably the playlist fragment's filter.addAction(MusicService.REFRESH); // If a playlist has changed, notify us filter.addAction(MusicService.PLAYLIST_CHANGED); // If there is an error playing a track filter.addAction(MusicService.TRACK_ERROR); registerReceiver(mPlaybackStatus, filter); } @Override protected void onStop() { super.onStop(); } @Override public void onResume() { super.onResume(); onMetaChanged(); } @Override public void onServiceConnected(final ComponentName name, final IBinder service) { mService = ITimberService.Stub.asInterface(service); onMetaChanged(); } @Override public void onServiceDisconnected(final ComponentName name) { mService = null; } @Override protected void onDestroy() { super.onDestroy(); // Unbind from the service if (mToken != null) { MusicPlayer.unbindFromService(mToken); mToken = null; } try { unregisterReceiver(mPlaybackStatus); } catch (final Throwable e) { } mMusicStateListener.clear(); } @Override public void onMetaChanged() { // Let the listener know to the meta chnaged for (final MusicStateListener listener : mMusicStateListener) { if (listener != null) { listener.onMetaChanged(); } } } @Override public void restartLoader() { // Let the listener know to update a list for (final MusicStateListener listener : mMusicStateListener) { if (listener != null) { listener.restartLoader(); } } } @Override public void onPlaylistChanged() { // Let the listener know to update a list for (final MusicStateListener listener : mMusicStateListener) { if (listener != null) { listener.onPlaylistChanged(); } } } public void setMusicStateListenerListener(final MusicStateListener status) { if (status == this) { throw new UnsupportedOperationException("Override the method, don't add a listener"); } if (status != null) { mMusicStateListener.add(status); } } public void removeMusicStateListenerListener(final MusicStateListener status) { if (status != null) { mMusicStateListener.remove(status); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main, menu); if (!TimberUtils.hasEffectsPanel(BaseActivity.this)) { menu.removeItem(R.id.action_equalizer); } ATE.applyMenu(this, getATEKey(), menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: super.onBackPressed(); return true; case R.id.action_settings: NavigationUtils.navigateToSettings(this); return true; case R.id.action_shuffle: Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { MusicPlayer.shuffleAll(BaseActivity.this); } }, 80); return true; case R.id.action_search: NavigationUtils.navigateToSearch(this); return true; case R.id.action_equalizer: NavigationUtils.navigateToEqualizer(this); return true; } return super.onOptionsItemSelected(item); } @Nullable @Override public String getATEKey() { return Helpers.getATEKey(this); } public void setPanelSlideListeners(SlidingUpPanelLayout panelLayout) { panelLayout.setPanelSlideListener(new SlidingUpPanelLayout.PanelSlideListener() { @Override public void onPanelSlide(View panel, float slideOffset) { View nowPlayingCard = QuickControlsFragment.topContainer; nowPlayingCard.setAlpha(1 - slideOffset); } @Override public void onPanelCollapsed(View panel) { View nowPlayingCard = QuickControlsFragment.topContainer; nowPlayingCard.setAlpha(1); } @Override public void onPanelExpanded(View panel) { View nowPlayingCard = QuickControlsFragment.topContainer; nowPlayingCard.setAlpha(0); } @Override public void onPanelAnchored(View panel) { } @Override public void onPanelHidden(View panel) { } }); } private final static class PlaybackStatus extends BroadcastReceiver { private final WeakReference<BaseActivity> mReference; public PlaybackStatus(final BaseActivity activity) { mReference = new WeakReference<BaseActivity>(activity); } @Override public void onReceive(final Context context, final Intent intent) { final String action = intent.getAction(); BaseActivity baseActivity = mReference.get(); if (baseActivity != null) { if (action.equals(MusicService.META_CHANGED)) { baseActivity.onMetaChanged(); } else if (action.equals(MusicService.PLAYSTATE_CHANGED)) { // baseActivity.mPlayPauseProgressButton.getPlayPauseButton().updateState(); } else if (action.equals(MusicService.REFRESH)) { baseActivity.restartLoader(); } else if (action.equals(MusicService.PLAYLIST_CHANGED)) { baseActivity.onPlaylistChanged(); } else if (action.equals(MusicService.TRACK_ERROR)) { final String errorMsg = context.getString(R.string.error_playing_track, intent.getStringExtra(MusicService.TrackErrorExtra.TRACK_NAME)); Toast.makeText(baseActivity, errorMsg, Toast.LENGTH_SHORT).show(); } } } } public class initQuickControls extends AsyncTask<String, Void, String> { @Override protected String doInBackground(String... params) { QuickControlsFragment fragment1 = new QuickControlsFragment(); FragmentManager fragmentManager1 = getSupportFragmentManager(); fragmentManager1.beginTransaction().replace(R.id.quickcontrols_container, fragment1) .commitAllowingStateLoss(); return "Executed"; } @Override protected void onPostExecute(String result) { // QuickControlsFragment.topContainer.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View v) { // NavigationUtils.navigateToNowplaying(BaseActivity.this, false); // } // }); } @Override protected void onPreExecute() { } } }