Merge pull request #1353 from lomash/1316_reworked

Homescreen widget support cont. #1316
This commit is contained in:
Victor Ng 2015-01-05 19:01:16 -05:00
Родитель 9d50b995e0 68e1b3935b
Коммит 94c77b8f3c
9 изменённых файлов: 263 добавлений и 8 удалений

Просмотреть файл

@ -1,7 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.mozilla.mozstumbler" >
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<!-- ANDROID_MOCK_LOCATION -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
@ -40,7 +39,7 @@
<activity
android:name=".client.subactivities.PreferencesScreen"
android:label="@string/action_preferences"
android:parentActivityName=".client.navdrawer.MainDrawerActivity" >
android:parentActivityName=".client.navdrawer.MainDrawerActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
@ -49,7 +48,7 @@
<activity
android:name=".client.subactivities.AboutActivity"
android:label="@string/action_about"
android:parentActivityName=".client.subactivities.PreferencesScreen" >
android:parentActivityName=".client.subactivities.PreferencesScreen">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
@ -59,7 +58,7 @@
<activity
android:name=".client.subactivities.LogActivity"
android:label="@string/title_activity_log"
android:parentActivityName=".client.subactivities.PreferencesScreen" >
android:parentActivityName=".client.subactivities.PreferencesScreen">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
@ -70,7 +69,7 @@
android:name=".client.subactivities.LeaderboardActivity"
android:label="@string/title_activity_leaderboard"
android:configChanges="orientation|screenSize|keyboard"
android:parentActivityName=".client.navdrawer.MainDrawerActivity" >
android:parentActivityName=".client.navdrawer.MainDrawerActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
@ -79,7 +78,7 @@
<activity
android:name=".client.subactivities.DeveloperActivity"
android:configChanges="orientation|screenSize|keyboardHidden"
android:parentActivityName=".client.navdrawer.MainDrawerActivity" >
android:parentActivityName=".client.navdrawer.MainDrawerActivity">
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
@ -95,6 +94,10 @@
android:value=".client.subactivities.PreferencesScreen" />
</activity>
<activity
android:name=".client.ToggleWidgetHelper"
android:label="@string/app_name"></activity>
<service
android:name=".client.ClientStumblerService"
android:icon="@drawable/ic_launcher"
@ -104,6 +107,18 @@
<receiver android:name=".service.uploadthread.UploadAlarmReceiver" />
<service android:name=".service.uploadthread.UploadAlarmReceiver$UploadAlarmService" />
<receiver
android:name=".client.ToggleWidgetProvider"
android:icon="@drawable/ic_launcher"
android:label="Mozilla Stumbler">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="@xml/toggle_widget_provider" />
</receiver>
<!-- For debugging the passive service -->
<!--
test with

Просмотреть файл

@ -0,0 +1,39 @@
package org.mozilla.mozstumbler.client;
import android.app.Activity;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.os.Bundle;
import android.view.View;
import android.widget.RemoteViews;
import org.mozilla.mozstumbler.R;
public class ToggleWidgetHelper extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
Bundle remoteViewsBundle = getIntent().getExtras();
RemoteViews remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.toggle_widget);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_widget_helper);
MainApp app = (MainApp) getApplication();
if (app.getService() == null) {
return;
}
boolean isScanning = app.getService().isScanning();
if (isScanning) {
remoteViews.setTextViewText(R.id.stumbler_info1, "0");
remoteViews.setTextViewText(R.id.stumbler_info2, "0");
remoteViews.setImageViewResource(R.id.toggleServiceButton, R.drawable.ic_status_scanning);
remoteViews.setViewVisibility(R.id.stumbler_info_bar, View.INVISIBLE);
app.stopScanning();
} else {
remoteViews.setTextViewText(R.id.stumbler_info1, "0");
remoteViews.setTextViewText(R.id.stumbler_info2, "0");
remoteViews.setImageViewResource(R.id.toggleServiceButton, R.drawable.ic_launcher);
remoteViews.setViewVisibility(R.id.stumbler_info_bar, View.VISIBLE);
app.startScanning();
}
(AppWidgetManager.getInstance(getApplicationContext())).updateAppWidget(new ComponentName(getApplicationContext(), ToggleWidgetProvider.class), remoteViews);
this.finish();
}
}

Просмотреть файл

@ -0,0 +1,48 @@
package org.mozilla.mozstumbler.client;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.RemoteViews;
import org.mozilla.mozstumbler.R;
public class ToggleWidgetProvider extends AppWidgetProvider{
private RemoteViews remoteViews;
private ComponentName watchWidget;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
for (int widgetId : appWidgetIds) {
remoteViews = new RemoteViews( context.getPackageName(), R.layout.toggle_widget );
watchWidget = new ComponentName( context, ToggleWidgetProvider.class );
Intent intentClick = new Intent(context,ToggleWidgetProvider.class);
intentClick.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, ""+appWidgetIds[0]);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, appWidgetIds[0],intentClick, 0);
remoteViews.setOnClickPendingIntent(R.id.toggleServiceButton, pendingIntent);
appWidgetManager.updateAppWidget( watchWidget, remoteViews );
}
}
@Override
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction() == null) {
Bundle extras = intent.getExtras();
if (extras != null) {
remoteViews = new RemoteViews(context.getPackageName(), R.layout.toggle_widget);
watchWidget = new ComponentName(context, ToggleWidgetProvider.class);
(AppWidgetManager.getInstance(context)).updateAppWidget(watchWidget, remoteViews);
AppWidgetProvider provider = new AppWidgetProvider();
Intent myIntent = new Intent(context, ToggleWidgetHelper.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(myIntent);
}
}
}
}

Просмотреть файл

@ -4,6 +4,8 @@
package org.mozilla.mozstumbler.client.navdrawer;
import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Build;
@ -21,6 +23,7 @@ import android.view.View;
import android.view.WindowManager;
import android.widget.CompoundButton;
import android.widget.Switch;
import android.widget.RemoteViews;
import org.mozilla.mozstumbler.BuildConfig;
import org.mozilla.mozstumbler.R;
@ -28,6 +31,7 @@ import org.mozilla.mozstumbler.client.ClientPrefs;
import org.mozilla.mozstumbler.client.ClientStumblerService;
import org.mozilla.mozstumbler.client.IMainActivity;
import org.mozilla.mozstumbler.client.MainApp;
import org.mozilla.mozstumbler.client.ToggleWidgetProvider;
import org.mozilla.mozstumbler.client.Updater;
import org.mozilla.mozstumbler.client.mapview.MapFragment;
import org.mozilla.mozstumbler.client.subactivities.FirstRunFragment;
@ -44,6 +48,7 @@ public class MainDrawerActivity
private MetricsView mMetricsView;
private MapFragment mMapFragment;
private MenuItem mMenuItemStartStop;
private RemoteViews remoteViews;
final CompoundButton.OnCheckedChangeListener mStartStopButtonListener =
new CompoundButton.OnCheckedChangeListener() {
@ -58,7 +63,7 @@ public class MainDrawerActivity
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
assert(findViewById(android.R.id.content) != null);
assert (findViewById(android.R.id.content) != null);
if (Build.VERSION.SDK_INT > 10) {
getWindow().setFlags(
@ -69,7 +74,8 @@ public class MainDrawerActivity
getSupportActionBar().setTitle(getString(R.string.app_name));
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle (
remoteViews = new RemoteViews(getApplicationContext().getPackageName(), R.layout.toggle_widget);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
@ -181,6 +187,7 @@ public class MainDrawerActivity
}
}
updateWidget(svc);
mMapFragment.dimToolbar();
}
@ -260,6 +267,7 @@ public class MainDrawerActivity
return;
}
updateWidget(service);
mMapFragment.formatTextView(R.id.text_cells_visible, "%d", service.getVisibleCellInfoCount());
mMapFragment.formatTextView(R.id.text_wifis_visible, "%d", service.getVisibleAPCount());
@ -296,4 +304,22 @@ public class MainDrawerActivity
public void stop() {
mMapFragment.stop();
}
private void updateWidget(ClientStumblerService service) {
boolean isScanning = service.isScanning();
if (isScanning) {
remoteViews.setImageViewResource(R.id.toggleServiceButton, R.drawable.ic_launcher);
remoteViews.setTextViewText(R.id.stumbler_info1, Integer.toString(service.getVisibleAPCount()));
remoteViews.setTextViewText(R.id.stumbler_info2, Integer.toString(service.getVisibleCellInfoCount()));
remoteViews.setTextViewText(R.id.stumbler_info3, Integer.toString(service.getObservationCount()));
remoteViews.setViewVisibility(R.id.stumbler_info_bar, View.VISIBLE);
} else {
remoteViews.setTextViewText(R.id.stumbler_info1, "0");
remoteViews.setTextViewText(R.id.stumbler_info2, "0");
remoteViews.setTextViewText(R.id.stumbler_info3, "0");
remoteViews.setImageViewResource(R.id.toggleServiceButton, R.drawable.ic_status_scanning);
remoteViews.setViewVisibility(R.id.stumbler_info_bar, View.INVISIBLE);
}
(AppWidgetManager.getInstance(getApplicationContext())).updateAppWidget(new ComponentName(getApplicationContext(), ToggleWidgetProvider.class), remoteViews);
}
}

Двоичные данные
android/src/main/res/drawable-hdpi/ic_widget.png Normal file

Двоичный файл не отображается.

После

Ширина:  |  Высота:  |  Размер: 23 KiB

Просмотреть файл

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>

Просмотреть файл

@ -0,0 +1,106 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/toggleService"
android:background="@xml/rounded_border">
<ImageView
android:id="@+id/toggleServiceButton"
android:layout_width="54dp"
android:layout_height="54dp"
android:paddingLeft="10dp"
android:layout_gravity="center_vertical"
android:checked="false"
android:scaleType="center"
android:src="@drawable/ic_status_scanning"/>
<LinearLayout
android:id="@+id/stumbler_info_bar"
android:layout_width="match_parent"
android:minHeight="54dp"
android:layout_height="54dp"
android:layout_gravity="center"
android:paddingLeft="4dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="40dp"
android:layout_weight="0.4"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="36dp"
android:minHeight="36dp"
android:layout_gravity="center"
android:layout_height="36dp"
android:src="@drawable/ic_wifi_flaticondotcom"/>
<TextView
android:id="@+id/stumbler_info1"
android:layout_width="match_parent"
android:layout_height="50dp"
android:minHeight="36dp"
android:layout_gravity="center_vertical"
android:textAlignment="gravity"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:textSize="16sp"
android:textColor="@android:color/black"
android:text="0" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:minHeight="40dp"
android:layout_height="36dp"
android:layout_weight="0.4"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="36dp"
android:minHeight="36dp"
android:layout_gravity="center"
android:layout_height="36dp"
android:src="@drawable/ic_cell_flaticondotcom"/>
<TextView
android:id="@+id/stumbler_info2"
android:layout_width="match_parent"
android:layout_height="50dp"
android:minHeight="36dp"
android:layout_gravity="center_vertical"
android:textAlignment="gravity"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:textSize="16sp"
android:textColor="@android:color/black"
android:text="0" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:minHeight="36dp"
android:layout_height="36dp"
android:layout_weight="0.4"
android:layout_gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="36dp"
android:minHeight="36dp"
android:layout_gravity="center"
android:layout_height="36dp"
android:src="@drawable/ic_binoculars_flaticondotcom"/>
<TextView
android:id="@+id/stumbler_info3"
android:layout_width="match_parent"
android:layout_height="50dp"
android:minHeight="36dp"
android:layout_gravity="center_vertical"
android:textAlignment="gravity"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:textSize="16sp"
android:textColor="@android:color/black"
android:text="0" />
</LinearLayout>
</LinearLayout>
</LinearLayout>

Просмотреть файл

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<corners android:radius="20dp"/>
<padding android:left="10dp" android:right="10dp" android:top="10dp" android:bottom="10dp"/>
<solid android:color="@android:color/white"/>
</shape>

Просмотреть файл

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialLayout="@layout/toggle_widget"
android:minHeight="40dp"
android:minWidth="272dp"
android:updatePeriodMillis="60000"
android:previewImage="@drawable/ic_widget">
</appwidget-provider>