Android Open Source - device-frame-generator Device Fragment






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)
 */* ww  w . j a va  2 s  .  co  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.fragments;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.InjectView;
import butterknife.OnClick;
import com.f2prateek.dart.InjectExtra;
import com.f2prateek.dfg.DeviceProvider;
import com.f2prateek.dfg.Events;
import com.f2prateek.dfg.R;
import com.f2prateek.dfg.core.AbstractGenerateFrameService;
import com.f2prateek.dfg.core.GenerateFrameService;
import com.f2prateek.dfg.model.Device;
import com.segment.analytics.Analytics;
import com.segment.analytics.Properties;
import com.squareup.otto.Subscribe;
import com.squareup.picasso.Picasso;
import de.keyboardsurfer.android.widget.crouton.Crouton;
import de.keyboardsurfer.android.widget.crouton.Style;
import java.util.List;
import javax.inject.Inject;

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

public class DeviceFragment extends BaseFragment {
  private static final String EXTRA_DEVICE = "device";
  private static final int RESULT_SELECT_PICTURE = 1;

  @Inject Picasso picasso;
  @Inject Analytics analytics;
  @Inject DeviceProvider deviceProvider;

  @InjectView(R.id.tv_device_resolution) TextView deviceResolutionText;
  @InjectView(R.id.tv_device_size) TextView deviceSizeText;
  @InjectView(R.id.tv_device_name) TextView deviceNameText;
  @InjectView(R.id.iv_device_thumbnail) ImageView deviceThumbnailText;
  @InjectView(R.id.iv_device_default) ImageView deviceDefaultText;

  @InjectExtra(EXTRA_DEVICE) Device device;

  public static DeviceFragment newInstance(Device device) {
    DeviceFragment f = new DeviceFragment();
    Bundle args = new Bundle();
    args.putParcelable(EXTRA_DEVICE, device);
    f.setArguments(args);
    f.setRetainInstance(true);
    return f;
  }

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

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_device, container, false);
  }

  @Override
  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    picasso.load(getResourceIdentifierForDrawable(getActivity(), device.getThumbnailResourceName()))
        .fit()
        .centerInside()
        .into(deviceThumbnailText);
    deviceDefaultText.bringToFront();
    deviceDefaultText.setImageResource(
        isDefault() ? R.drawable.ic_action_star_selected : R.drawable.ic_action_star);
    deviceSizeText.setText(device.physicalSize() + "\" @ " + device.density());
    deviceNameText.setText(device.name());
    deviceResolutionText.setText(device.realSize().x() + "x" + device.realSize().y());
  }

  @OnClick(R.id.iv_device_default)
  public void updateDefaultDevice() {
    if (isDefault()) {
      return;
    }
    deviceProvider.saveDefaultDevice(device);
    bus.post(new Events.DefaultDeviceUpdated(device));
  }

  @Subscribe
  public void onDefaultDeviceUpdated(Events.DefaultDeviceUpdated event) {
    deviceDefaultText.post(new Runnable() {
      @Override public void run() {
        deviceDefaultText.setImageResource(
            isDefault() ? R.drawable.ic_action_star_selected : R.drawable.ic_action_star);
      }
    });
  }

  private boolean isDefault() {
    return deviceProvider.getDefaultDevice().id().equals(device.id());
  }

  @OnClick(R.id.iv_device_thumbnail)
  public void getScreenshotImageFromUser() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    if (isAvailable(activityContext, intent)) {
      startActivityForResult(Intent.createChooser(intent, getString(R.string.select_picture)),
          RESULT_SELECT_PICTURE);
    } else {
      Crouton.makeText(getActivity(), R.string.no_apps_available, Style.ALERT).show();
    }
  }

  /**
   * Check if any apps are installed on the app to receive this intent.
   */
  public static boolean isAvailable(Context ctx, Intent intent) {
    final PackageManager mgr = ctx.getPackageManager();
    List<ResolveInfo> list = mgr.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
  }

  @OnClick(R.id.tv_device_name)
  public void openDevicePage() {
    Properties properties = new Properties();
    device.into(properties);
    analytics.track("Clicked Device Website", properties);
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setData(Uri.parse(device.url()));
    startActivity(i);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == RESULT_SELECT_PICTURE && resultCode == Activity.RESULT_OK) {
      if (data == null) {
        return;
      }
      Uri selectedImageUri = data.getData();
      Intent intent = new Intent(getActivity(), GenerateFrameService.class);
      intent.putExtra(AbstractGenerateFrameService.KEY_EXTRA_DEVICE, device);
      intent.putExtra(GenerateFrameService.KEY_EXTRA_SCREENSHOT, selectedImageUri);
      getActivity().startService(intent);
    } else {
      super.onActivityResult(requestCode, resultCode, data);
    }
  }
}




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