diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 46c6490d880..44555db8b0a 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -72,6 +72,7 @@ + diff --git a/src/com/android/settings/dashboard/DashboardSummary.java b/src/com/android/settings/dashboard/DashboardSummary.java index be5fbdf15f7..6caf052bc60 100644 --- a/src/com/android/settings/dashboard/DashboardSummary.java +++ b/src/com/android/settings/dashboard/DashboardSummary.java @@ -38,6 +38,7 @@ import com.android.settings.dashboard.conditional.FocusRecyclerView; import com.android.settings.dashboard.conditional.FocusRecyclerView.FocusListener; import com.android.settings.dashboard.suggestions.SuggestionDismissController; import com.android.settings.dashboard.suggestions.SuggestionFeatureProvider; +import com.android.settings.dashboard.suggestions.SuggestionControllerMixin; import com.android.settings.dashboard.suggestions.SuggestionsChecks; import com.android.settings.overlay.FeatureFactory; import com.android.settings.widget.ActionBarShadowController; @@ -60,7 +61,6 @@ public class DashboardSummary extends InstrumentedFragment private static final int MAX_WAIT_MILLIS = 700; private static final String TAG = "DashboardSummary"; - private static final String EXTRA_SCROLL_POSITION = "scroll_position"; private final Handler mHandler = new Handler(); @@ -72,6 +72,7 @@ public class DashboardSummary extends InstrumentedFragment private SuggestionParser mSuggestionParser; private LinearLayoutManager mLayoutManager; private SuggestionsChecks mSuggestionsChecks; + private SuggestionControllerMixin mSuggestionControllerMixin; private DashboardFeatureProvider mDashboardFeatureProvider; private SuggestionFeatureProvider mSuggestionFeatureProvider; private boolean isOnCategoriesChangedCalled; @@ -82,6 +83,12 @@ public class DashboardSummary extends InstrumentedFragment return MetricsEvent.DASHBOARD_SUMMARY; } + @Override + public void onAttach(Context context) { + super.onAttach(context); + mSuggestionControllerMixin = new SuggestionControllerMixin(context, getLifecycle()); + } + @Override public void onCreate(Bundle savedInstanceState) { long startTime = System.currentTimeMillis(); @@ -196,7 +203,7 @@ public class DashboardSummary extends InstrumentedFragment mDashboard.setHasFixedSize(true); mDashboard.setListener(this); mAdapter = new DashboardAdapter(getContext(), bundle, mConditionManager.getConditions(), - mSuggestionParser, this /* SuggestionDismissController.Callback */); + mSuggestionParser, this /* SuggestionDismissController.Callback */); mDashboard.setAdapter(mAdapter); mDashboard.setItemAnimator(new DashboardItemAnimator()); mSummaryLoader.setSummaryConsumer(mAdapter); diff --git a/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixin.java b/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixin.java new file mode 100644 index 00000000000..0142203499e --- /dev/null +++ b/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixin.java @@ -0,0 +1,144 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * 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.android.settings.dashboard.suggestions; + +import android.annotation.Nullable; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; +import android.content.ServiceConnection; +import android.os.IBinder; +import android.os.RemoteException; +import android.service.settings.suggestions.ISuggestionService; +import android.service.settings.suggestions.Suggestion; +import android.support.annotation.VisibleForTesting; +import android.util.Log; + +import com.android.settingslib.core.lifecycle.Lifecycle; +import com.android.settingslib.core.lifecycle.LifecycleObserver; +import com.android.settingslib.core.lifecycle.events.OnStart; +import com.android.settingslib.core.lifecycle.events.OnStop; + +import java.util.List; + +/** + * Manages IPC communication to SettingsIntelligence for suggestion related services. + */ +public class SuggestionControllerMixin implements LifecycleObserver, OnStart, OnStop { + + private static final String TAG = "SuggestionCtrlMixin"; + private static final boolean DEBUG = false; + + private final Context mContext; + private final Intent mServiceIntent; + private final ServiceConnection mServiceConnection; + + private ISuggestionService mRemoteService; + + public SuggestionControllerMixin(Context context, Lifecycle lifecycle) { + mContext = context.getApplicationContext(); + mServiceIntent = new Intent().setComponent( + new ComponentName( + "com.android.settings.intelligence", + "com.android.settings.intelligence.suggestions.SuggestionService")); + mServiceConnection = createServiceConnection(); + if (lifecycle != null) { + lifecycle.addObserver(this); + } + } + + @Override + public void onStart() { + if (!isEnabled()) { + Log.w(TAG, "Feature not enabled, skipping"); + return; + } + mContext.bindServiceAsUser(mServiceIntent, mServiceConnection, Context.BIND_AUTO_CREATE, + android.os.Process.myUserHandle()); + } + + @Override + public void onStop() { + if (mRemoteService != null) { + mRemoteService = null; + mContext.unbindService(mServiceConnection); + } + } + + public boolean isEnabled() { + // TODO: Set up feature flag + return true; + } + + /** + * Get setting suggestions. + */ + @Nullable + public List getSuggestions() { + if (!isReady()) { + return null; + } + try { + return mRemoteService.getSuggestions(); + } catch (RemoteException e) { + Log.w(TAG, "Error when calling getSuggestion()", e); + return null; + } + } + + /** + * Whether or not the manager is ready + */ + private boolean isReady() { + return mRemoteService != null; + } + + @VisibleForTesting + void onServiceConnected() { + // TODO: Call API to get data from a loader instead of in current thread. + final List data = getSuggestions(); + Log.d(TAG, "data size " + (data == null ? 0 : data.size())); + } + + private void onServiceDisconnected() { + + } + + /** + * Create a new {@link ServiceConnection} object to handle service connect/disconnect event. + */ + private ServiceConnection createServiceConnection() { + return new ServiceConnection() { + + @Override + public void onServiceConnected(ComponentName name, IBinder service) { + if (DEBUG) { + Log.d(TAG, "Service is connected"); + } + mRemoteService = ISuggestionService.Stub.asInterface(service); + SuggestionControllerMixin.this.onServiceConnected(); + } + + @Override + public void onServiceDisconnected(ComponentName name) { + mRemoteService = null; + SuggestionControllerMixin.this.onServiceDisconnected(); + } + }; + } + +} diff --git a/tests/robotests/src/android/service/settings/suggestions/ISuggestionService.java b/tests/robotests/src/android/service/settings/suggestions/ISuggestionService.java new file mode 100644 index 00000000000..f4f5a519bd0 --- /dev/null +++ b/tests/robotests/src/android/service/settings/suggestions/ISuggestionService.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * 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 android.service.settings.suggestions; + +import java.util.List; + +public interface ISuggestionService { + List getSuggestions(); +} diff --git a/tests/robotests/src/android/service/settings/suggestions/Suggestion.java b/tests/robotests/src/android/service/settings/suggestions/Suggestion.java new file mode 100644 index 00000000000..df7a8aed962 --- /dev/null +++ b/tests/robotests/src/android/service/settings/suggestions/Suggestion.java @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * 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 android.service.settings.suggestions; + +public class Suggestion { +} diff --git a/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixinTest.java b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixinTest.java new file mode 100644 index 00000000000..10611846bc2 --- /dev/null +++ b/tests/robotests/src/com/android/settings/dashboard/suggestions/SuggestionControllerMixinTest.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * 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.android.settings.dashboard.suggestions; + +import static com.google.common.truth.Truth.assertThat; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.os.RemoteException; +import android.service.settings.suggestions.ISuggestionService; + +import com.android.settings.TestConfig; +import com.android.settings.testutils.SettingsRobolectricTestRunner; +import com.android.settingslib.core.lifecycle.Lifecycle; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.robolectric.annotation.Config; +import org.robolectric.util.ReflectionHelpers; + +@RunWith(SettingsRobolectricTestRunner.class) +@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) +public class SuggestionControllerMixinTest { + + @Mock + private Context mContext; + @Mock + private ISuggestionService mRemoteService; + private Lifecycle mLifecycle; + private SuggestionControllerMixin mMixin; + + + @Before + public void setUp() { + MockitoAnnotations.initMocks(this); + mLifecycle = new Lifecycle(); + when(mContext.getApplicationContext()).thenReturn(mContext); + } + + @Test + public void verifyIsEnabled() { + mMixin = new SuggestionControllerMixin(mContext, mLifecycle); + assertThat(mMixin.isEnabled()).isTrue(); + } + + @Test + public void onServiceConnected_shouldGetSuggestion() { + mMixin = new SuggestionControllerMixin(mContext, mLifecycle); + ReflectionHelpers.setField(mMixin, "mRemoteService", mRemoteService); + mMixin.onServiceConnected(); + + verify(mRemoteService).getSuggestions(); + } + +}