Example usage for android.appwidget AppWidgetHost AppWidgetHost

List of usage examples for android.appwidget AppWidgetHost AppWidgetHost

Introduction

In this page you can find the example usage for android.appwidget AppWidgetHost AppWidgetHost.

Prototype

public AppWidgetHost(Context context, int hostId) 

Source Link

Usage

From source file:net.niyonkuru.koodroid.appwidget.WidgetConfigureActivity.java

@Override
public void onPause() {
    super.onPause();

    mAlertDialog.dismiss(); // avoid leaking window

    String action = getIntent().getAction();
    if (mResultCode == RESULT_CANCELED
            && (action != null && action.equals(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE))) {
        /* avoid phantom widgets */
        AppWidgetHost host = new AppWidgetHost(this, 1);
        host.deleteAppWidgetId(mAppWidgetId);
    } else {/*from  w w w  .j  av a  2  s. c  om*/
        Intent intent = IntentUtils.createWidgetIntent(this, AppWidgetManager.ACTION_APPWIDGET_UPDATE);
        intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, new int[] { mAppWidgetId });

        sendBroadcast(intent);
    }
}

From source file:com.farmerbb.taskbar.service.DashboardService.java

@SuppressLint("RtlHardcoded")
private void drawDashboard() {
    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM,
            PixelFormat.TRANSLUCENT);//from  www. j ava  2s.co  m

    // Initialize views
    layout = new LinearLayout(this);
    layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    layout.setVisibility(View.GONE);
    layout.setAlpha(0);

    SharedPreferences pref = U.getSharedPreferences(this);
    int width = pref.getInt("dashboard_width",
            getApplicationContext().getResources().getInteger(R.integer.dashboard_width));
    int height = pref.getInt("dashboard_height",
            getApplicationContext().getResources().getInteger(R.integer.dashboard_height));

    boolean isPortrait = getApplicationContext().getResources()
            .getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    boolean isLandscape = getApplicationContext().getResources()
            .getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;

    if (isPortrait) {
        columns = height;
        rows = width;
    }

    if (isLandscape) {
        columns = width;
        rows = height;
    }

    maxSize = columns * rows;

    int backgroundTint = U.getBackgroundTint(this);
    int accentColor = U.getAccentColor(this);
    int accentColorAlt = accentColor;
    accentColorAlt = ColorUtils.setAlphaComponent(accentColorAlt, Color.alpha(accentColorAlt) / 2);

    int cellCount = 0;

    for (int i = 0; i < columns; i++) {
        LinearLayout layout2 = new LinearLayout(this);
        layout2.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, 1));
        layout2.setOrientation(LinearLayout.VERTICAL);

        for (int j = 0; j < rows; j++) {
            DashboardCell cellLayout = (DashboardCell) View.inflate(this, R.layout.dashboard, null);
            cellLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT, 1));
            cellLayout.setBackgroundColor(backgroundTint);
            cellLayout.setOnClickListener(cellOcl);
            cellLayout.setOnHoverListener(cellOhl);

            TextView empty = (TextView) cellLayout.findViewById(R.id.empty);
            empty.setBackgroundColor(accentColorAlt);
            empty.setTextColor(accentColor);

            Bundle bundle = new Bundle();
            bundle.putInt("cellId", cellCount);

            cellLayout.setTag(bundle);
            cells.put(cellCount, cellLayout);
            cellCount++;

            layout2.addView(cellLayout);
        }

        layout.addView(layout2);
    }

    mAppWidgetManager = AppWidgetManager.getInstance(this);
    mAppWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);
    mAppWidgetHost.startListening();

    for (int i = 0; i < maxSize; i++) {
        int appWidgetId = pref.getInt("dashboard_widget_" + Integer.toString(i), -1);
        if (appWidgetId != -1)
            addWidget(appWidgetId, i, false);
        else if (pref.getBoolean("dashboard_widget_" + Integer.toString(i) + "_placeholder", false))
            addPlaceholder(i);
    }

    mAppWidgetHost.stopListening();

    LocalBroadcastManager.getInstance(this).unregisterReceiver(toggleReceiver);
    LocalBroadcastManager.getInstance(this).unregisterReceiver(addWidgetReceiver);
    LocalBroadcastManager.getInstance(this).unregisterReceiver(removeWidgetReceiver);
    LocalBroadcastManager.getInstance(this).unregisterReceiver(hideReceiver);

    LocalBroadcastManager.getInstance(this).registerReceiver(toggleReceiver,
            new IntentFilter("com.farmerbb.taskbar.TOGGLE_DASHBOARD"));
    LocalBroadcastManager.getInstance(this).registerReceiver(addWidgetReceiver,
            new IntentFilter("com.farmerbb.taskbar.ADD_WIDGET_COMPLETED"));
    LocalBroadcastManager.getInstance(this).registerReceiver(removeWidgetReceiver,
            new IntentFilter("com.farmerbb.taskbar.REMOVE_WIDGET_COMPLETED"));
    LocalBroadcastManager.getInstance(this).registerReceiver(hideReceiver,
            new IntentFilter("com.farmerbb.taskbar.HIDE_DASHBOARD"));

    windowManager.addView(layout, params);

    new Handler().postDelayed(() -> {
        int paddingSize = getResources().getDimensionPixelSize(R.dimen.icon_size);

        switch (U.getTaskbarPosition(DashboardService.this)) {
        case "top_vertical_left":
        case "bottom_vertical_left":
            layout.setPadding(paddingSize, 0, 0, 0);
            break;
        case "top_left":
        case "top_right":
            layout.setPadding(0, paddingSize, 0, 0);
            break;
        case "top_vertical_right":
        case "bottom_vertical_right":
            layout.setPadding(0, 0, paddingSize, 0);
            break;
        case "bottom_left":
        case "bottom_right":
            layout.setPadding(0, 0, 0, paddingSize);
            break;
        }
    }, 100);
}

From source file:de.spiritcroc.modular_remote.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setTitle(R.string.title_activity_main);

    Util.init(this);

    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
    pages = new ArrayList<>();

    String savedFragments = sharedPreferences.getString(getSavedFragmentsKey(), "");
    if (DEBUG)/*from w  w w  .  java  2  s.  com*/
        Log.v(LOG_TAG, "savedFragments: " + savedFragments);

    pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
    viewPager = (CustomViewPager) findViewById(R.id.view_pager);

    forceOrientation = getIntent().getIntExtra(EXTRA_FORCE_ORIENTATION, -1);

    restoreContentFromRecreationKey(savedFragments);
    pages.get(0).setMenuEnabled(true);

    fragmentPagerAdapter = new CustomFragmentPagerAdapter(this, getFragmentManager());
    viewPager.setAdapter(fragmentPagerAdapter);

    viewPager.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop,
                int oldRight, int oldBottom) {
            boolean newY = Util.newY(viewPager);
            boolean newX = Util.newX(viewPager);
            if (newY || newX) {
                viewPager.post(new Runnable() {
                    @Override
                    public void run() {
                        resizeContent();
                    }
                });
            }
        }
    });

    fullscreen = sharedPreferences.getBoolean(Preferences.FULLSCREEN, false);
    hideNavigationBar = sharedPreferences.getBoolean(Preferences.HIDE_NAVIGATION_BAR, false);
    hideActionBar = sharedPreferences.getBoolean(Preferences.HIDE_ACTION_BAR, false);

    decorView = getWindow().getDecorView();
    decorView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
        @Override
        public void onSystemUiVisibilityChange(int visibility) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                boolean visible = (visibility & View.SYSTEM_UI_FLAG_HIDE_NAVIGATION) == 0;
                if (visible) {
                    delayedHide();
                }
            }
        }
    });

    appWidgetManager = AppWidgetManager.getInstance(this);
    appWidgetHost = new AppWidgetHost(this, APPWIDGET_HOST_ID);

    tcpConnectionManager = TcpConnectionManager.getInstance(getApplicationContext());
    tcpConnectionManager.recreateConnectionsFromRecreationKey(
            sharedPreferences.getString(Preferences.SAVED_CONNECTIONS, ""));

    if (sharedPreferences.getInt(Preferences.SEEN_GREETING_VERSION, 0) < GreetingDialog.VERSION) {
        new GreetingDialog().show(getFragmentManager(), "GreetingDialog");
    } else if (SetupGridSizeDialog.shouldShow(sharedPreferences)) {
        new SetupGridSizeDialog().show(getFragmentManager(), "SetupGridSizeDialog");
    }

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
            | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

    if (DEBUG) {
        getFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
            @Override
            public void onBackStackChanged() {
                Log.v(LOG_TAG, "Back stack count: " + getFragmentManager().getBackStackEntryCount());
            }
        });
    }

    if (getIntent().getBooleanExtra(EXTRA_RESTARTED_FROM_EDIT_MODE, false)) {
        enterEditMode();
    }
}