Refector TestProvider


#1

github.com/crystalDf/The-Big-Ne … ovider.git

I have made 3 changes.

1.make TestProviderService extend IntentService instead of Service
2.use LocationReceiver instead of inner interface Listener
3.add mLocationManager.requestLocationUpdates(TEST_PROVIDER, 0, 0, pi)

TestProviderFragment.java

package com.star.testprovider;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.IntentFilter;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class TestProviderFragment extends Fragment {

    private Button mStartStopButton;
    private TextView mStatusTextView;

    private RunManager mRunManager;

    private Location mLastLocation;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);

        mRunManager = RunManager.getInstance(getActivity());
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_test_provider, container, false);

        mStartStopButton = (Button) v.findViewById(R.id.startStopButton);
        mStartStopButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mRunManager.isTestProviderOn()) {
                    mRunManager.stopTestProviderLocationUpdates();
                } else {
                    mRunManager.startTestProviderLocationUpdates();
                }

                updateUI();
            }
        });

        mStatusTextView = (TextView) v.findViewById(R.id.statusTextView);

        return v;
    }

    private void updateUI() {
        boolean started = mRunManager.isTestProviderOn();

        if (started) {
            mStartStopButton.setText(R.string.stop);
        } else {
            mStartStopButton.setText(R.string.start);
        }

        if (started && mLastLocation != null) {
            String statusText = "Latitude: " + mLastLocation.getLatitude() + "\n" +
                            "Longitude: " + mLastLocation.getLongitude() + "\n" +
                            "Altitude: " + mLastLocation.getAltitude();
            mStatusTextView.setText(statusText);
        } else {
            mStatusTextView.setText("");
        }
    }

    private BroadcastReceiver mLocationReceiver = new LocationReceiver() {
        @Override
        protected void onLocationReceived(Context context, Location location) {
            super.onLocationReceived(context, location);

            mLastLocation = location;
            if (isVisible()) {
                updateUI();
            }
        }

        @Override
        protected void onProviderEnabledChanged(boolean enabled) {
            super.onProviderEnabledChanged(enabled);

            int toastText = enabled ? R.string.testProvider_enabled : R.string.testProvider_disabled;
            Toast.makeText(getActivity(), toastText, Toast.LENGTH_LONG).show();
        }
    };

    @Override
    public void onStart() {
        super.onStart();
        getActivity().registerReceiver(mLocationReceiver, new IntentFilter(RunManager.ACTION_LOCATION));
    }

    @Override
    public void onStop() {
        getActivity().unregisterReceiver(mLocationReceiver);
        super.onStop();
    }
}

TestProviderService.java

package com.star.testprovider;


import android.app.AlarmManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class TestProviderService extends IntentService {

    private static final String TAG = "TestProviderService";

    private static final int UPDATE_INTERVAL = 1000;

    private static int sRunIndex = 0;

    public TestProviderService() {
        super(TAG);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        RunManager runManager = RunManager.getInstance(TestProviderService.this);
        runManager.publishLocation(sRunIndex++);
    }

    public static void setServiceAlarm(Context context, boolean isOn) {
        Intent i = new Intent(context, TestProviderService.class);

        PendingIntent pi = PendingIntent.getService(context, 0, i, 0);

        AlarmManager alarmManager = (AlarmManager)
                context.getSystemService(Context.ALARM_SERVICE);

        if (isOn) {
            alarmManager.setRepeating(AlarmManager.RTC, System.currentTimeMillis(),
                    UPDATE_INTERVAL, pi);
        } else {
            alarmManager.cancel(pi);
            pi.cancel();
        }
    }
}

RunManager.java

package com.star.testprovider;


import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationManager;

import java.util.ArrayList;
import java.util.List;

public class RunManager {

    private static final String TAG = "RunManager";

    public static final String ACTION_LOCATION =
            "com.star.testprovider.ACTION_LOCATION";

    private static final String TEST_PROVIDER = "TEST_PROVIDER";

    private static RunManager sRunManager;

    private Context mAppContext;

    private LocationManager mLocationManager;

    private List<Location> mLocationList;

    private RunManager(Context appContext) {
        mAppContext = appContext;
        mLocationManager = (LocationManager)
                mAppContext.getSystemService(Context.LOCATION_SERVICE);

        initLocationData();
    }

    public static RunManager getInstance(Context context) {
        if (sRunManager == null) {
            synchronized (RunManager.class) {
                if (sRunManager == null) {
                    sRunManager = new RunManager(context);
                }
            }
        }
        return sRunManager;
    }

    private PendingIntent getLocationPendingIntent(boolean shouldCreate) {
        Intent broadcast = new Intent(ACTION_LOCATION);
        int flags = shouldCreate ? 0 : PendingIntent.FLAG_NO_CREATE;

        return PendingIntent.getBroadcast(mAppContext, 0, broadcast, flags);
    }

    public void startTestProviderLocationUpdates() {
        if (isTestProviderOn()) {
            mLocationManager.removeTestProvider(TEST_PROVIDER);
        }

        mLocationManager.addTestProvider(TEST_PROVIDER, false, false, false, false,
                true, false, false, 0, 0);

        mLocationManager.setTestProviderEnabled(TEST_PROVIDER, true);
        mLocationManager.setTestProviderStatus(TEST_PROVIDER,
                GpsStatus.GPS_EVENT_STARTED, null, System.currentTimeMillis());

        TestProviderService.setServiceAlarm(mAppContext, true);

        PendingIntent pi = getLocationPendingIntent(true);

        mLocationManager.requestLocationUpdates(TEST_PROVIDER, 0, 0, pi);
    }

    public void stopTestProviderLocationUpdates() {

        PendingIntent pi = getLocationPendingIntent(false);

        if (pi != null) {
            mLocationManager.removeUpdates(pi);
            pi.cancel();
        }

        mLocationManager.setTestProviderEnabled(TEST_PROVIDER, false);

        mLocationManager.removeTestProvider(TEST_PROVIDER);

        TestProviderService.setServiceAlarm(mAppContext, false);
    }

    public boolean isTestProviderOn() {
        return mLocationManager.getProvider(TEST_PROVIDER) != null;
    }

    public void publishLocation(int i) {
        Location location = mLocationList.get(i % mLocationList.size());
        location.setTime(System.currentTimeMillis());
        mLocationManager.setTestProviderLocation(TEST_PROVIDER, location);
    }

    private Location buildLocation(double latitude, double longitude, double altitude) {
        Location location = new Location(TEST_PROVIDER);
        location.setLatitude(latitude);
        location.setLongitude(longitude);
        location.setAltitude(altitude);

        return location;
    }

    private void initLocationData() {
        mLocationList = new ArrayList<>();

        mLocationList.add(buildLocation(33.751459, -84.3238770, 309));
        mLocationList.add(buildLocation(33.7514752, -84.3238663, 304));
        mLocationList.add(buildLocation(33.7514859, -84.3238610, 303));
        mLocationList.add(buildLocation(33.7514752, -84.32385, 303));
        mLocationList.add(buildLocation(33.7514859, -84.3237590, 287));
        mLocationList.add(buildLocation(33.7514859, -84.3237376, 286));
        mLocationList.add(buildLocation(33.7514805, -84.3237537, 286));
        mLocationList.add(buildLocation(33.7514430, -84.3237537, 283));
        mLocationList.add(buildLocation(33.7514108, -84.3237483, 286));
        mLocationList.add(buildLocation(33.7514162, -84.3237590, 287));
        mLocationList.add(buildLocation(33.7514269, -84.3237751, 287));
        mLocationList.add(buildLocation(33.7514376, -84.3237859, 287));
        mLocationList.add(buildLocation(33.7514698, -84.3238180, 283));
        mLocationList.add(buildLocation(33.7514805, -84.3238288, 282));
        mLocationList.add(buildLocation(33.7515181, -84.323887, 280));
        mLocationList.add(buildLocation(33.7515234, -84.3238985, 279));
        mLocationList.add(buildLocation(33.7515342, -84.3239146, 279));
        mLocationList.add(buildLocation(33.7515449, -84.3239307, 279));
        mLocationList.add(buildLocation(33.7515503, -84.3239468, 280));
        mLocationList.add(buildLocation(33.7515556, -84.323957, 280));
        mLocationList.add(buildLocation(33.751566, -84.3239682, 280));
        mLocationList.add(buildLocation(33.7515717, -84.3239790, 280));
        mLocationList.add(buildLocation(33.7515825, -84.3239897, 280));
        mLocationList.add(buildLocation(33.75159323, -84.3240058, 280));
        mLocationList.add(buildLocation(33.75160932, -84.3240165, 280));
        mLocationList.add(buildLocation(33.75161468, -84.3240272, 280));
        mLocationList.add(buildLocation(33.7510514, -84.3247300, 277));
        mLocationList.add(buildLocation(33.751062, -84.3247246, 276));
        mLocationList.add(buildLocation(33.75108897, -84.3247032, 276));
        mLocationList.add(buildLocation(33.7510782, -84.3246924, 280));
        mLocationList.add(buildLocation(33.7510836, -84.3247032, 282));
        mLocationList.add(buildLocation(33.75109970, -84.324697, 281));
        mLocationList.add(buildLocation(33.75112652, -84.324697, 283));
        mLocationList.add(buildLocation(33.75115334, -84.3246924, 281));
        mLocationList.add(buildLocation(33.7511640, -84.3246871, 284));
        mLocationList.add(buildLocation(33.7511748, -84.3246871, 284));
        mLocationList.add(buildLocation(33.7511855, -84.3246871, 284));
        mLocationList.add(buildLocation(33.7511962, -84.3246871, 284));
        mLocationList.add(buildLocation(33.751206, -84.3246871, 285));
        mLocationList.add(buildLocation(33.7512177, -84.3246763, 286));
        mLocationList.add(buildLocation(33.7512284, -84.3246763, 287));
        mLocationList.add(buildLocation(33.7512391, -84.3246763, 288));
        mLocationList.add(buildLocation(33.7512499, -84.3246817, 287));
        mLocationList.add(buildLocation(33.75126, -84.3246763, 287));
        mLocationList.add(buildLocation(33.7512713, -84.3246763, 287));
        mLocationList.add(buildLocation(33.7512820, -84.3246710, 287));
        mLocationList.add(buildLocation(33.7512981, -84.3246710, 287));
        mLocationList.add(buildLocation(33.751314, -84.3246763, 286));
        mLocationList.add(buildLocation(33.7513303, -84.3246710, 286));
        mLocationList.add(buildLocation(33.75135183, -84.324660, 287));
        mLocationList.add(buildLocation(33.75136792, -84.3246495, 287));
        mLocationList.add(buildLocation(33.75137865, -84.3246495, 287));
        mLocationList.add(buildLocation(33.75140011, -84.3246388, 286));
        mLocationList.add(buildLocation(33.7514108, -84.3246281, 286));
        mLocationList.add(buildLocation(33.7514269, -84.3246227, 286));
        mLocationList.add(buildLocation(33.7514376, -84.3246120, 285));
        mLocationList.add(buildLocation(33.7514537, -84.324606, 285));
        mLocationList.add(buildLocation(33.7514644, -84.3246012, 285));
        mLocationList.add(buildLocation(33.7514752, -84.3245959, 285));
        mLocationList.add(buildLocation(33.7514805, -84.3245851, 285));
        mLocationList.add(buildLocation(33.7515020, -84.3245798, 286));
        mLocationList.add(buildLocation(33.751512, -84.3245744, 286));
        mLocationList.add(buildLocation(33.7515234, -84.3245637, 286));
        mLocationList.add(buildLocation(33.7515288, -84.324553, 287));
        mLocationList.add(buildLocation(33.7515395, -84.3245476, 287));
        mLocationList.add(buildLocation(33.7515503, -84.3245422, 287));
        mLocationList.add(buildLocation(33.751566, -84.3245315, 288));
        mLocationList.add(buildLocation(33.7515717, -84.3245208, 288));
        mLocationList.add(buildLocation(33.7515825, -84.3245154, 288));
        mLocationList.add(buildLocation(33.75158786, -84.324499, 290));
        mLocationList.add(buildLocation(33.75160396, -84.3244940, 290));
        mLocationList.add(buildLocation(33.75161468, -84.3244832, 291));
        mLocationList.add(buildLocation(33.75162541, -84.3244725, 292));
        mLocationList.add(buildLocation(33.75164151, -84.324461, 292));
        mLocationList.add(buildLocation(33.75165224, -84.324445, 293));
        mLocationList.add(buildLocation(33.7516629, -84.3244349, 293));
        mLocationList.add(buildLocation(33.7516736, -84.3244242, 294));
        mLocationList.add(buildLocation(33.7516790, -84.3244135, 294));
        mLocationList.add(buildLocation(33.7516897, -84.3244028, 294));
        mLocationList.add(buildLocation(33.7516897, -84.3243813, 294));
        mLocationList.add(buildLocation(33.7516844, -84.3243598, 295));
        mLocationList.add(buildLocation(33.7516790, -84.3243438, 295));
        mLocationList.add(buildLocation(33.7516844, -84.3243277, 295));
        mLocationList.add(buildLocation(33.7516790, -84.3243062, 295));
        mLocationList.add(buildLocation(33.7516736, -84.3242955, 296));
        mLocationList.add(buildLocation(33.7516683, -84.3242794, 297));
        mLocationList.add(buildLocation(33.7516629, -84.3242686, 296));
        mLocationList.add(buildLocation(33.75165224, -84.3242526, 297));
        mLocationList.add(buildLocation(33.75164151, -84.3242365, 297));
        mLocationList.add(buildLocation(33.75162541, -84.3242204, 297));
        mLocationList.add(buildLocation(33.75162005, -84.3242043, 297));
        mLocationList.add(buildLocation(33.75161468, -84.3241882, 298));
        mLocationList.add(buildLocation(33.75160396, -84.3241775, 299));
        mLocationList.add(buildLocation(33.75159859, -84.3241667, 298));
        mLocationList.add(buildLocation(33.75159323, -84.324156, 299));
        mLocationList.add(buildLocation(33.7515771, -84.3241453, 299));
        mLocationList.add(buildLocation(33.751566, -84.3241345, 298));
        mLocationList.add(buildLocation(33.7515556, -84.3241184, 298));
        mLocationList.add(buildLocation(33.751566, -84.3241077, 297));
        mLocationList.add(buildLocation(33.7515556, -84.3240863, 296));
        mLocationList.add(buildLocation(33.7515503, -84.3240702, 296));
        mLocationList.add(buildLocation(33.7515449, -84.3240541, 296));
        mLocationList.add(buildLocation(33.7515288, -84.3240380, 297));
        mLocationList.add(buildLocation(33.7515234, -84.3240272, 297));
        mLocationList.add(buildLocation(33.7515181, -84.3240058, 297));
        mLocationList.add(buildLocation(33.7515074, -84.323995, 297));
        mLocationList.add(buildLocation(33.7515020, -84.3239790, 296));
        mLocationList.add(buildLocation(33.7514913, -84.3239629, 295));
        mLocationList.add(buildLocation(33.7514805, -84.3239521, 294));
        mLocationList.add(buildLocation(33.7514644, -84.323941, 294));
        mLocationList.add(buildLocation(33.7514537, -84.3239307, 293));
        mLocationList.add(buildLocation(33.7514483, -84.3239146, 294));
        mLocationList.add(buildLocation(33.7514483, -84.3238985, 293));
        mLocationList.add(buildLocation(33.7514483, -84.3238770, 293));
        mLocationList.add(buildLocation(33.7514483, -84.32385, 292));
        mLocationList.add(buildLocation(33.7514537, -84.3238395, 291));
        mLocationList.add(buildLocation(33.751459, -84.3238234, 291));
        mLocationList.add(buildLocation(33.7514698, -84.3238019, 291));
        mLocationList.add(buildLocation(33.7514805, -84.3237859, 291));
        mLocationList.add(buildLocation(33.7514859, -84.3237751, 291));
        mLocationList.add(buildLocation(33.7514913, -84.3237644, 291));
        mLocationList.add(buildLocation(33.7514913, -84.3237483, 290));
        mLocationList.add(buildLocation(33.7515020, -84.323742, 290));
        mLocationList.add(buildLocation(33.7515074, -84.3237322, 290));
        mLocationList.add(buildLocation(33.7515181, -84.3237268, 290));
        mLocationList.add(buildLocation(33.7515288, -84.3237161, 290));
        mLocationList.add(buildLocation(33.7515342, -84.323705, 290));
        mLocationList.add(buildLocation(33.7515181, -84.3237000, 290));
        mLocationList.add(buildLocation(33.7515074, -84.3237161, 290));
        mLocationList.add(buildLocation(33.7514966, -84.3237376, 290));
        mLocationList.add(buildLocation(33.7514913, -84.3237537, 290));
        mLocationList.add(buildLocation(33.7514859, -84.3237751, 290));
        mLocationList.add(buildLocation(33.7514966, -84.3237751, 290));
        mLocationList.add(buildLocation(33.7515020, -84.323796, 290));
        mLocationList.add(buildLocation(33.751512, -84.323796, 290));
        mLocationList.add(buildLocation(33.7515234, -84.3238127, 289));
        mLocationList.add(buildLocation(33.7514966, -84.3237912, 289));
        mLocationList.add(buildLocation(33.7514859, -84.3237805, 289));
        mLocationList.add(buildLocation(33.751459, -84.3237698, 289));
        mLocationList.add(buildLocation(33.7514537, -84.3237805, 289));
        mLocationList.add(buildLocation(33.7514430, -84.3237751, 290));
    }

}

LocationReceiver.java

package com.star.testprovider;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.util.Log;

public class LocationReceiver extends BroadcastReceiver {

    private static final String TAG = "LocationReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Location location = intent.getParcelableExtra(LocationManager.KEY_LOCATION_CHANGED);

        if (location != null) {
            onLocationReceived(context, location);
        } else if (intent.hasExtra(LocationManager.KEY_PROVIDER_ENABLED)) {
            boolean enabled = intent.getBooleanExtra(LocationManager.KEY_PROVIDER_ENABLED, false);
            onProviderEnabledChanged(enabled);
        }
    }

    protected void onLocationReceived(Context context, Location location) {
        Log.d(TAG, this + " Got location from " + location.getProvider() + ": " +
                location.getLatitude() + ", " + location.getLongitude());
    }

    protected void onProviderEnabledChanged(boolean enabled) {
        Log.d(TAG, "Provider " + (enabled ? "enabled" : "disabled"));
    }
}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.star.testprovider" >

    <uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".TestProviderActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".TestProviderService" />
        <receiver android:name=".LocationReceiver"
                  android:exported="false">
            <intent-filter>
                <action android:name="com.star.testprovider.ACTION_LOCATION" />
            </intent-filter>
        </receiver>
    </application>

</manifest>