android: Use Fragment class from the support library to avoid deprecation warnings
authorTobias Brunner <tobias@strongswan.org>
Thu, 28 Apr 2016 07:21:06 +0000 (09:21 +0200)
committerTobias Brunner <tobias@strongswan.org>
Mon, 2 May 2016 14:41:50 +0000 (16:41 +0200)
For instance, onAttach() with an Activitiy as first argument was deprecated
with API level 23.  However, the overload with a Context as first argument
does obviously not get called on older API levels.  Luckily, the classes
provided by the support library handle that for us.

src/frontends/android/app/src/main/java/org/strongswan/android/ui/ImcStateFragment.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/LogFragment.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/MainActivity.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/RemediationInstructionFragment.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/RemediationInstructionsActivity.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/RemediationInstructionsFragment.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnProfileListFragment.java
src/frontends/android/app/src/main/java/org/strongswan/android/ui/VpnStateFragment.java

index 7328693..bf5eaf6 100644 (file)
@@ -15,9 +15,6 @@
 
 package org.strongswan.android.ui;
 
-import android.app.Fragment;
-import android.app.FragmentManager;
-import android.app.FragmentTransaction;
 import android.app.Service;
 import android.content.ComponentName;
 import android.content.Context;
@@ -25,6 +22,9 @@ import android.content.Intent;
 import android.content.ServiceConnection;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
 import android.support.v4.content.ContextCompat;
 import android.view.GestureDetector;
 import android.view.LayoutInflater;
index 8740e0c..2f62407 100644 (file)
 
 package org.strongswan.android.ui;
 
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.StringReader;
-
-import org.strongswan.android.R;
-import org.strongswan.android.logic.CharonVpnService;
-
-import android.app.Fragment;
 import android.os.Bundle;
 import android.os.FileObserver;
 import android.os.Handler;
+import android.support.v4.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.TextView;
 
+import org.strongswan.android.R;
+import org.strongswan.android.logic.CharonVpnService;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.StringReader;
+
 public class LogFragment extends Fragment implements Runnable
 {
        private String mLogFilePath;
index 3d53240..a6c6681 100644 (file)
@@ -18,9 +18,6 @@
 package org.strongswan.android.ui;
 
 import android.app.Dialog;
-import android.app.Fragment;
-import android.app.FragmentManager;
-import android.app.FragmentTransaction;
 import android.app.Service;
 import android.content.ActivityNotFoundException;
 import android.content.ComponentName;
@@ -31,6 +28,9 @@ import android.net.VpnService;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.support.v4.app.Fragment;
+import android.support.v4.app.FragmentManager;
+import android.support.v4.app.FragmentTransaction;
 import android.support.v7.app.ActionBar;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.app.AppCompatActivity;
@@ -317,7 +317,7 @@ public class MainActivity extends AppCompatActivity implements OnVpnProfileSelec
         */
        public void removeFragmentByTag(String tag)
        {
-               FragmentManager fm = getFragmentManager();
+               FragmentManager fm = getSupportFragmentManager();
                Fragment login = fm.findFragmentByTag(tag);
                if (login != null)
                {
index 04c288b..9223479 100644 (file)
@@ -18,8 +18,8 @@ package org.strongswan.android.ui;
 import org.strongswan.android.R;
 import org.strongswan.android.logic.imc.RemediationInstruction;
 
-import android.app.ListFragment;
 import android.os.Bundle;
+import android.support.v4.app.ListFragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
index 7e7e870..7765a2e 100644 (file)
@@ -38,7 +38,7 @@ public class RemediationInstructionsActivity extends AppCompatActivity implement
                {       /* only update if we're not restoring */
                        return;
                }
-               RemediationInstructionsFragment frag = (RemediationInstructionsFragment)getFragmentManager().findFragmentById(R.id.remediation_instructions_fragment);
+               RemediationInstructionsFragment frag = (RemediationInstructionsFragment)getSupportFragmentManager().findFragmentById(R.id.remediation_instructions_fragment);
                if (frag != null)
                {       /* two-pane layout, update fragment */
                        Bundle extras = getIntent().getExtras();
@@ -49,7 +49,7 @@ public class RemediationInstructionsActivity extends AppCompatActivity implement
                {       /* one-pane layout, create fragment */
                        frag = new RemediationInstructionsFragment();
                        frag.setArguments(getIntent().getExtras());
-                       getFragmentManager().beginTransaction().add(R.id.fragment_container, frag).commit();
+                       getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, frag).commit();
                }
        }
 
@@ -60,7 +60,7 @@ public class RemediationInstructionsActivity extends AppCompatActivity implement
                {
                        case android.R.id.home:
                                /* one-pane layout, pop possible fragment from stack, finish otherwise */
-                               if (!getFragmentManager().popBackStackImmediate())
+                               if (!getSupportFragmentManager().popBackStackImmediate())
                                {
                                        finish();
                                }
@@ -74,7 +74,7 @@ public class RemediationInstructionsActivity extends AppCompatActivity implement
        @Override
        public void onRemediationInstructionSelected(RemediationInstruction instruction)
        {
-               RemediationInstructionFragment frag = (RemediationInstructionFragment)getFragmentManager().findFragmentById(R.id.remediation_instruction_fragment);
+               RemediationInstructionFragment frag = (RemediationInstructionFragment)getSupportFragmentManager().findFragmentById(R.id.remediation_instruction_fragment);
 
                if (frag != null)
                {       /* two-pane layout, update directly */
@@ -87,7 +87,7 @@ public class RemediationInstructionsActivity extends AppCompatActivity implement
                        args.putParcelable(RemediationInstructionFragment.ARG_REMEDIATION_INSTRUCTION, instruction);
                        frag.setArguments(args);
 
-                       getFragmentManager().beginTransaction().replace(R.id.fragment_container, frag).addToBackStack(null).commit();
+                       getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, frag).addToBackStack(null).commit();
                        getSupportActionBar().setTitle(instruction.getTitle());
                }
        }
index 86467dc..a2beffd 100644 (file)
 
 package org.strongswan.android.ui;
 
-import java.util.ArrayList;
+import android.content.Context;
+import android.os.Bundle;
+import android.support.v4.app.ListFragment;
+import android.view.View;
+import android.widget.ListView;
 
 import org.strongswan.android.R;
 import org.strongswan.android.logic.imc.RemediationInstruction;
 import org.strongswan.android.ui.adapter.RemediationInstructionAdapter;
 
-import android.app.Activity;
-import android.app.ListFragment;
-import android.os.Bundle;
-import android.view.View;
-import android.widget.ListView;
+import java.util.ArrayList;
 
 public class RemediationInstructionsFragment extends ListFragment
 {
@@ -65,13 +65,13 @@ public class RemediationInstructionsFragment extends ListFragment
        }
 
        @Override
-       public void onAttach(Activity activity)
+       public void onAttach(Context context)
        {
-               super.onAttach(activity);
+               super.onAttach(context);
 
-               if (activity instanceof OnRemediationInstructionSelectedListener)
+               if (context instanceof OnRemediationInstructionSelectedListener)
                {
-                       mListener = (OnRemediationInstructionSelectedListener)activity;
+                       mListener = (OnRemediationInstructionSelectedListener)context;
                }
        }
 
index a23df05..d8d99ff 100644 (file)
 
 package org.strongswan.android.ui;
 
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-
-import org.strongswan.android.R;
-import org.strongswan.android.data.VpnProfile;
-import org.strongswan.android.data.VpnProfileDataSource;
-import org.strongswan.android.ui.adapter.VpnProfileAdapter;
-
 import android.app.Activity;
-import android.app.Fragment;
 import android.content.Context;
 import android.content.Intent;
 import android.content.res.TypedArray;
 import android.os.Bundle;
+import android.support.v4.app.Fragment;
 import android.util.AttributeSet;
 import android.view.ActionMode;
 import android.view.LayoutInflater;
@@ -46,6 +37,15 @@ import android.widget.AdapterView.OnItemClickListener;
 import android.widget.ListView;
 import android.widget.Toast;
 
+import org.strongswan.android.R;
+import org.strongswan.android.data.VpnProfile;
+import org.strongswan.android.data.VpnProfileDataSource;
+import org.strongswan.android.ui.adapter.VpnProfileAdapter;
+
+import java.util.ArrayList;
+import java.util.HashSet;
+import java.util.List;
+
 public class VpnProfileListFragment extends Fragment
 {
        private static final int ADD_REQUEST = 1;
@@ -66,10 +66,10 @@ public class VpnProfileListFragment extends Fragment
        }
 
        @Override
-       public void onInflate(Activity activity, AttributeSet attrs, Bundle savedInstanceState)
+       public void onInflate(Context context, AttributeSet attrs, Bundle savedInstanceState)
        {
-               super.onInflate(activity, attrs, savedInstanceState);
-               TypedArray a = activity.obtainStyledAttributes(attrs, R.styleable.Fragment);
+               super.onInflate(context, attrs, savedInstanceState);
+               TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Fragment);
                mReadOnly = a.getBoolean(R.styleable.Fragment_read_only, false);
                a.recycle();
        }
@@ -126,13 +126,13 @@ public class VpnProfileListFragment extends Fragment
        }
 
        @Override
-       public void onAttach(Activity activity)
+       public void onAttach(Context context)
        {
-               super.onAttach(activity);
+               super.onAttach(context);
 
-               if (activity instanceof OnVpnProfileSelectedListener)
+               if (context instanceof OnVpnProfileSelectedListener)
                {
-                       mListener = (OnVpnProfileSelectedListener)activity;
+                       mListener = (OnVpnProfileSelectedListener)context;
                }
        }
 
index c49e5cc..10bf2c3 100644 (file)
@@ -17,7 +17,6 @@
 
 package org.strongswan.android.ui;
 
-import android.app.Fragment;
 import android.app.ProgressDialog;
 import android.app.Service;
 import android.content.ComponentName;
@@ -27,6 +26,7 @@ import android.content.Intent;
 import android.content.ServiceConnection;
 import android.os.Bundle;
 import android.os.IBinder;
+import android.support.v4.app.Fragment;
 import android.support.v4.content.ContextCompat;
 import android.support.v7.app.AlertDialog;
 import android.view.LayoutInflater;