[Android] [PATCH] Avoid to use Application Context

Jean-Baptiste Kempf jb at videolan.org
Thu Dec 11 12:19:27 CET 2014


On 11 Dec, Geoffrey Métais wrote :
> diff --git a/vlc-android/src/org/videolan/vlc/PhoneStateReceiver.java b/vlc-android/src/org/videolan/vlc/PhoneStateReceiver.java
> index cb43bea..b6db147 100644
> --- a/vlc-android/src/org/videolan/vlc/PhoneStateReceiver.java
> +++ b/vlc-android/src/org/videolan/vlc/PhoneStateReceiver.java
> @@ -29,17 +29,19 @@ public class PhoneStateReceiver extends BroadcastReceiver {
>  
>      @Override
>      public void onReceive(Context context, Intent intent) {
> +        if (context == null)
> +            context = VLCApplication.getAppContext();
>          String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
>  
>          if (state.equals(TelephonyManager.EXTRA_STATE_RINGING) ||
>                  state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
>              Intent newIntent = new Intent(VLCApplication.INCOMING_CALL_INTENT);
> -            VLCApplication.getAppContext().sendBroadcast(newIntent);
> +            context.sendBroadcast(newIntent);
>          }
>  
>          if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
>              Intent newIntent = new Intent(VLCApplication.CALL_ENDED_INTENT);
> -            VLCApplication.getAppContext().sendBroadcast(newIntent);
> +            context.sendBroadcast(newIntent);
>          }
>      }
>  

OK.

> diff --git a/vlc-android/src/org/videolan/vlc/audio/AudioService.java b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
> index 6fc79a0..f04725f 100644
> --- a/vlc-android/src/org/videolan/vlc/audio/AudioService.java
> +++ b/vlc-android/src/org/videolan/vlc/audio/AudioService.java
> @@ -219,8 +219,7 @@ public class AudioService extends Service {
>       */
>      @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
>      public void setUpRemoteControlClient() {
> -        Context context = VLCApplication.getAppContext();
> -        AudioManager audioManager = (AudioManager)context.getSystemService(AUDIO_SERVICE);
> +        AudioManager audioManager = (AudioManager)getSystemService(AUDIO_SERVICE);
>  
>          if (LibVlcUtil.isICSOrLater()) {
>              audioManager.registerMediaButtonEventReceiver(mRemoteControlClientReceiverComponent);
> @@ -228,7 +227,7 @@ public class AudioService extends Service {
>              if (mRemoteControlClient == null) {
>                  Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
>                  mediaButtonIntent.setComponent(mRemoteControlClientReceiverComponent);
> -                PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context, 0, mediaButtonIntent, 0);
> +                PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
>  
>                  // create and register the remote control client
>                  mRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
> @@ -638,7 +637,7 @@ public class AudioService extends Service {
>          hideNotification(false);
>  
>          // Switch to the video player & don't lose the currently playing stream
> -        VideoPlayerActivity.start(VLCApplication.getAppContext(), MRL, title, index, true);
> +        VideoPlayerActivity.start(this, MRL, title, index, true);
>      }
>  
>      private void executeUpdate() {

OK.

> diff --git a/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java b/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
> index c78781e..02a9c52 100644
> --- a/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
> +++ b/vlc-android/src/org/videolan/vlc/gui/BrowserAdapter.java
> @@ -151,7 +151,7 @@ public class BrowserAdapter extends ArrayAdapter<File>
>          if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
>              // Show "sdcard" for the user's folder when running in multi-user
>              if (file.getAbsolutePath().equals(Environment.getExternalStorageDirectory().getPath())) {
> -                return VLCApplication.getAppContext().getString(R.string.internal_memory);
> +                return getContext().getString(R.string.internal_memory);
>              }
>          }
>          return file.getName();

OK.

> diff --git a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
> index 3c1ed56..98aeee7 100644
> --- a/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
> +++ b/vlc-android/src/org/videolan/vlc/gui/DirectoryAdapter.java
> @@ -254,6 +254,7 @@ public class DirectoryAdapter extends BaseAdapter {
>      }
>  
>      private LayoutInflater mInflater;
> +    private Context mContext;

No, you shouldn't store Context.

>      private DirectoryAdapter.Node mRootNode;
>      private DirectoryAdapter.Node mCurrentNode;
>      private String mCurrentDir;
> @@ -276,6 +277,7 @@ public class DirectoryAdapter extends BaseAdapter {
>          mCurrentNode = mRootNode;
>          SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(activityContext);
>          mAlignMode = Integer.valueOf(preferences.getString("audio_title_alignment", "0"));
> +        mContext = activityContext;
>      }
>  
>      @Override
> @@ -307,8 +309,6 @@ public class DirectoryAdapter extends BaseAdapter {
>          DirectoryViewHolder holder;
>          View v = convertView;
>  
> -        Context context = VLCApplication.getAppContext();
> -
>          /* If view not created */
>          if (v == null) {
>              v = mInflater.inflate(R.layout.directory_view_item, parent, false);
> @@ -334,20 +334,20 @@ public class DirectoryAdapter extends BaseAdapter {
>              holder.title.setText(selectedNode.getVisibleName());
>  
>          if(selectedNode.name.equals(".."))
> -            holderText = context.getString(R.string.parent_folder);
> +            holderText = mContext.getString(R.string.parent_folder);
>          else if(!selectedNode.isFile()) {
>              int folderCount = selectedNode.subfolderCount();
>              int mediaFileCount = selectedNode.subfilesCount();
>              holderText = "";
>  
>              if(folderCount > 0)
> -                holderText += context.getResources().getQuantityString(
> +                holderText += mContext.getResources().getQuantityString(
>                          R.plurals.subfolders_quantity, folderCount, folderCount
>                  );
>              if(folderCount > 0 && mediaFileCount > 0)
>                  holderText += ", ";
>              if(mediaFileCount > 0)
> -                holderText += context.getResources().getQuantityString(
> +                holderText += mContext.getResources().getQuantityString(
>                          R.plurals.mediafiles_quantity, mediaFileCount,
>                          mediaFileCount);
>          }
> @@ -497,7 +497,7 @@ public class DirectoryAdapter extends BaseAdapter {
>          if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
>              // Show "sdcard" for the user's folder when running in multi-user
>              if (file.getAbsolutePath().equals(Environment.getExternalStorageDirectory().getPath())) {
> -                return VLCApplication.getAppContext().getString(R.string.internal_memory);
> +                return mContext.getString(R.string.internal_memory);
>              }
>          }
>          return file.getName();
> diff --git a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
> index f24577c..afa0fc3 100644
> --- a/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
> +++ b/vlc-android/src/org/videolan/vlc/gui/HistoryAdapter.java
> @@ -102,7 +102,7 @@ public class HistoryAdapter extends BaseAdapter {
>          holderText = m.getSubtitle();
>  
>          holder.text.setText(holderText);
> -        Bitmap b = AudioUtil.getCover(VLCApplication.getAppContext(), m, 64);
> +        Bitmap b = AudioUtil.getCover(v.getContext(), m, 64);
>          if(b != null)
>              holder.icon.setImageBitmap(b);

OK.

> diff --git a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
> index d0212c6..b7e3d51 100644
> --- a/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
> +++ b/vlc-android/src/org/videolan/vlc/util/VLCInstance.java
> @@ -49,7 +49,7 @@ public class VLCInstance {
>                  @Override
>                  public void onNativeCrash() {
>                      Intent i = new Intent(context, NativeCrashActivity.class);
> -                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
> +                    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
>                      i.putExtra("PID", android.os.Process.myPid());
>                      context.startActivity(i);
>                  }

This is different, no?

With my kindest regards,

-- 
Jean-Baptiste Kempf
http://www.jbkempf.com/ - +33 672 704 734
Sent from my Electronic Device


More information about the Android mailing list