Android Open Source - device-frame-generator Main Activity






From Project

Back to project page device-frame-generator.

License

The source code is released under:

Apache License

If you think the Android project device-frame-generator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright 2014 Prateek Srivastava (@f2prateek)
 *//  w  w  w.jav  a  2s .  c  o  m
 * 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.f2prateek.dfg.ui.activities;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import butterknife.InjectView;
import com.astuetz.PagerSlidingTabStrip;
import com.f2prateek.dfg.DeviceProvider;
import com.f2prateek.dfg.Events;
import com.f2prateek.dfg.R;
import com.f2prateek.dfg.model.Device;
import com.f2prateek.dfg.prefs.GlareEnabled;
import com.f2prateek.dfg.prefs.ShadowEnabled;
import com.f2prateek.dfg.prefs.model.BooleanPreference;
import com.f2prateek.dfg.ui.DeviceFragmentPagerAdapter;
import com.f2prateek.dfg.ui.fragments.AboutFragment;
import com.f2prateek.ln.Ln;
import com.readystatesoftware.systembartint.SystemBarTintManager;
import com.segment.analytics.Analytics;
import com.segment.analytics.Properties;
import com.segment.analytics.Traits;
import com.squareup.otto.Subscribe;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
import javax.inject.Inject;

import static com.f2prateek.dfg.Utils.getColor;

public class MainActivity extends BaseActivity {
  @Inject @GlareEnabled BooleanPreference glareEnabled;
  @Inject @ShadowEnabled BooleanPreference shadowEnabled;
  @Inject DeviceProvider deviceProvider;
  @Inject WindowManager windowManager;
  @Inject Analytics analytics;

  @InjectView(R.id.pager) ViewPager pager;
  @InjectView(R.id.tabs) PagerSlidingTabStrip tabStrip;

  DeviceFragmentPagerAdapter pagerAdapter;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
    getActionBar().setCustomView(R.layout.action_bar_custom);

    analytics.screen(null, "Device");

    inflateView(R.layout.activity_main);

    // create our manager instance after the content view is set
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      // This could have been set in the theme as well, the launching animation
      // looks a bit jarring (white status bar with white icons on launch)
      // This looks a bit cleaner, turning from black to grey
      setTranslucentStatus(true);

      SystemBarTintManager tintManager = new SystemBarTintManager(this);
      tintManager.setStatusBarTintEnabled(true);
      tintManager.setStatusBarTintResource(R.color.action_bar_color);
    }

    pagerAdapter = new DeviceFragmentPagerAdapter(getFragmentManager(), deviceProvider.asList());
    pager.setAdapter(pagerAdapter);
    pager.setCurrentItem(pagerAdapter.getDeviceIndex(deviceProvider.getDefaultDevice()));
    tabStrip.setTextColor(getColor(this, R.color.title_text_color, Color.WHITE));
    tabStrip.setViewPager(pager);
  }

  @TargetApi(19)
  private void setTranslucentStatus(boolean on) {
    Window win = getWindow();
    WindowManager.LayoutParams winParams = win.getAttributes();
    final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
    if (on) {
      winParams.flags |= bits;
    } else {
      winParams.flags &= ~bits;
    }
    win.setAttributes(winParams);
  }

  @Override public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.activity_main, menu);
    initMenuItem(menu.findItem(R.id.menu_checkbox_glare), glareEnabled);
    initMenuItem(menu.findItem(R.id.menu_checkbox_shadow), shadowEnabled);
    return true;
  }

  void initMenuItem(MenuItem menuItem, BooleanPreference preference) {
    menuItem.setChecked(preference.get());
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // items aren't toggled automatically, so we
      // just use the opposite of the state we're in
      case R.id.menu_checkbox_glare:
        updateGlareSetting(!item.isChecked());
        return true;
      case R.id.menu_checkbox_shadow:
        updateShadowSetting(!item.isChecked());
        return true;
      case R.id.menu_match_device:
        analytics.track("Match Device Menu Item Clicked");
        Device device = deviceProvider.find(windowManager);
        if (device == null) {
          Crouton.makeText(this, R.string.no_matching_device, Style.ALERT).show();
        } else {
          pager.setCurrentItem(pagerAdapter.getDeviceIndex(device));
        }
        return true;
      case R.id.menu_about:
        analytics.track("About Menu Item Clicked");
        final AboutFragment fragment = new AboutFragment();
        fragment.show(getFragmentManager(), "about");
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }

  public void updateGlareSetting(boolean newSettingEnabled) {
    analytics.track("Glare " + (newSettingEnabled ? "Enabled" : "Disabled"));
    analytics.identify(new Traits().putValue("glare_enabled", newSettingEnabled));

    updateBooleanPreference(newSettingEnabled, glareEnabled, getString(R.string.glare_enabled),
        getString(R.string.glare_disabled));
  }

  public void updateShadowSetting(boolean newSettingEnabled) {
    analytics.track("Shadow " + (newSettingEnabled ? "Enabled" : "Disabled"));
    analytics.identify(new Traits().putValue("shadow_enabled", newSettingEnabled));

    updateBooleanPreference(newSettingEnabled, shadowEnabled, getString(R.string.shadow_enabled),
        getString(R.string.shadow_disabled));
  }

  /**
   * Update a boolean preference with the new value.
   * Displays some text to the user dependending on the preference.
   */
  void updateBooleanPreference(boolean newSettingEnabled, BooleanPreference booleanPreference,
      String enabledText, String disabledText) {
    booleanPreference.set(newSettingEnabled);
    if (newSettingEnabled) {
      Crouton.makeText(this, enabledText, Style.CONFIRM).show();
    } else {
      Crouton.makeText(this, disabledText, Style.ALERT).show();
    }
    Ln.d("Setting updated to %s", newSettingEnabled);
    invalidateOptionsMenu();
  }

  @Subscribe
  public void onDefaultDeviceUpdated(Events.DefaultDeviceUpdated event) {
    Ln.d("Device updated to %s", event.newDevice.name());
    Properties properties = new Properties();
    event.newDevice.into(properties);
    analytics.track("Updated Default Device", properties);
    Traits traits = new Traits();
    event.newDevice.into(traits);
    analytics.identify(traits);

    Crouton.makeText(this, getString(R.string.saved_as_default_message, event.newDevice.name()),
        Style.CONFIRM).show();
    // This might be from the application class, so update the position as well
    // the application class runs it on the main thread currently, so this more for a
    // future improvement
    pager.setCurrentItem(pagerAdapter.getDeviceIndex(event.newDevice));
    invalidateOptionsMenu();
  }

  @Subscribe
  public void onSingleImageProcessed(final Events.SingleImageProcessed event) {
    Crouton.makeText(this, getString(R.string.single_screenshot_saved, event.device.name()),
        Style.INFO).setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        Intent launchIntent = new Intent(Intent.ACTION_VIEW);
        launchIntent.setDataAndType(event.uri, "image/png");
        launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(launchIntent);
      }
    }).show();
  }

  @Subscribe
  public void onMultipleImagesProcessed(final Events.MultipleImagesProcessed event) {
    if (event.uriList.size() == 0) {
      return;
    }
    Crouton.makeText(this,
        getString(R.string.multiple_screenshots_saved, event.uriList.size(), event.device.name()),
        Style.INFO).setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        Intent launchIntent = new Intent(Intent.ACTION_VIEW);
        launchIntent.setDataAndType(event.uriList.get(0), "image/png");
        launchIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(launchIntent);
      }
    }).show();
  }
}




Java Source Code List

com.f2prateek.dfg.AnalyticsKey.java
com.f2prateek.dfg.AppConstants.java
com.f2prateek.dfg.CrashlyticsLn.java
com.f2prateek.dfg.DFGApplicationModule.java
com.f2prateek.dfg.DFGApplication.java
com.f2prateek.dfg.DebugDFGApplicationModule.java
com.f2prateek.dfg.DeviceModule.java
com.f2prateek.dfg.DeviceProvider.java
com.f2prateek.dfg.Events.java
com.f2prateek.dfg.ForApplication.java
com.f2prateek.dfg.Modules.java
com.f2prateek.dfg.Modules.java
com.f2prateek.dfg.Utils.java
com.f2prateek.dfg.core.AbstractGenerateFrameService.java
com.f2prateek.dfg.core.DeviceFrameGenerator.java
com.f2prateek.dfg.core.GenerateFrameService.java
com.f2prateek.dfg.core.GenerateMultipleFramesService.java
com.f2prateek.dfg.model.Bounds.java
com.f2prateek.dfg.model.Device.java
com.f2prateek.dfg.model.Orientation.java
com.f2prateek.dfg.prefs.DebugPreferencesModule.java
com.f2prateek.dfg.prefs.DefaultDevice.java
com.f2prateek.dfg.prefs.FirstRun.java
com.f2prateek.dfg.prefs.GlareEnabled.java
com.f2prateek.dfg.prefs.PreferencesModule.java
com.f2prateek.dfg.prefs.ShadowEnabled.java
com.f2prateek.dfg.prefs.debug.AnimationSpeed.java
com.f2prateek.dfg.prefs.debug.PicassoDebugging.java
com.f2prateek.dfg.prefs.debug.PixelGridEnabled.java
com.f2prateek.dfg.prefs.debug.PixelRatioEnabled.java
com.f2prateek.dfg.prefs.debug.ScalpelEnabled.java
com.f2prateek.dfg.prefs.debug.ScalpelWireframeEnabled.java
com.f2prateek.dfg.prefs.debug.SeenDebugDrawer.java
com.f2prateek.dfg.prefs.model.BooleanPreference.java
com.f2prateek.dfg.prefs.model.IntPreference.java
com.f2prateek.dfg.prefs.model.StringPreference.java
com.f2prateek.dfg.ui.ActivityHierarchyServer.java
com.f2prateek.dfg.ui.AppContainer.java
com.f2prateek.dfg.ui.BindableAdapter.java
com.f2prateek.dfg.ui.DebugUiModule.java
com.f2prateek.dfg.ui.DeviceFragmentPagerAdapter.java
com.f2prateek.dfg.ui.SocketActivityHierarchyServer.java
com.f2prateek.dfg.ui.UiModule.java
com.f2prateek.dfg.ui.activities.BaseActivity.java
com.f2prateek.dfg.ui.activities.MainActivity.java
com.f2prateek.dfg.ui.activities.ReceiverActivity.java
com.f2prateek.dfg.ui.debug.AnimationSpeedAdapter.java
com.f2prateek.dfg.ui.debug.ContextualDebugActions.java
com.f2prateek.dfg.ui.debug.DebugAppContainer.java
com.f2prateek.dfg.ui.debug.HierarchyTreeChangeListener.java
com.f2prateek.dfg.ui.fragments.AboutFragment.java
com.f2prateek.dfg.ui.fragments.BaseFragment.java
com.f2prateek.dfg.ui.fragments.DeviceFragment.java
com.f2prateek.dfg.ui.widgets.ForegroundImageView.java