Please wait while loading the page. You need to enable javascript to show the content.

n00b trek
Home
May 14th, 2012 — James

I just started Android programming. While trying to get device id for AdMob test device, I came across posts that indicated I needed to use the device id from telephony manager. The id did not work for AdMob. But here is the code to get the id from telephony manager.

import android.content.Context;
import android.telephony.TelephonyManager;
import android.util.Log;

public class DesiDeviceUtility {
	private static final String TAG = "DesiDeviceUtility";
	public static String getDeviceId(Context context)
	{
		final TelephonyManager tm =
                     (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
		String deviceId = tm.getDeviceId();
		Log.d(TAG, deviceId);
		return deviceId;
	}
}

Calling this method from an activity:

adRequest.addTestDevice(DesiDeviceUtility.getDeviceId(getBaseContext()));

January 30th, 2012 — James
// http://en.wikipedia.org/wiki/Mandelbrot_set
// Original Mandelbrot code by Jon Skeet
using System;
using System.Linq;
using System.Drawing.Imaging;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Threading;
class MainForm : Form
{
    const string AppTitle = "Mandelbrot";
    BackgroundColorGenerator worker;
    PictureBox pb;
    MainForm()
    {
        this.InitializeComponent();
        this.InitializeBackgroundWorker();
    }
    void InitializeComponent()
    {
        this.FormBorderStyle = FormBorderStyle.FixedSingle;
        this.Text = AppTitle;
        this.pb = new PictureBox();
        this.pb.Image = MandelbrotGenerator.Generate();
        this.pb.Dock = DockStyle.Fill;
        this.ClientSize = this.pb.Image.Size;
        this.Controls.Add(pb);
        this.Load += new EventHandler(this.MainForm_Load);
    }
    void InitializeBackgroundWorker()
    {
        this.worker = new BackgroundColorGenerator();
        this.worker.WorkerReportsProgress = true;
        this.worker.WorkerSupportsCancellation = true;
        this.worker.DoWork += new DoWorkEventHandler(this.worker.GenerateColor);
        this.worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
    }
    void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        ColorPalette palette = (ColorPalette)e.UserState;
        if (null != palette)
        {
            this.pb.Image.Palette = palette;
            this.pb.Invalidate(true);
        }
    }
    void MainForm_Load(object sender, System.EventArgs e)
    {
        this.worker.RunWorkerAsync(this.pb.Image.Palette);
    }
    public static void Main()
    {
        MainForm form = new MainForm();
        Application.Run(form);
    }
    class BackgroundColorGenerator : BackgroundWorker
    {
        const int Timeout = 1000;
        public void GenerateColor(object sender, DoWorkEventArgs args)
        {
            ColorPalette palette = (ColorPalette)args.Argument;
            Random rand = new Random();
            while (!this.CancellationPending)
            {
                palette.Entries[0] = Color.FromArgb((rand.Next(255) * rand.Next(7)) % 256,
                    (rand.Next(255) * rand.Next(7)) % 256,
                    (rand.Next(255) * rand.Next(7)) % 256);
                for (int i = 1; i < 256; i++)
                {
                    palette.Entries[i] = Color.FromArgb((rand.Next(i) * rand.Next(7)) % 256,
                        (rand.Next(i) * rand.Next(7)) % 256,
                        (rand.Next(i) * rand.Next(7)) % 256);
                }
                this.ReportProgress(0, palette);
                Thread.Sleep(BackgroundColorGenerator.Timeout);
            }
        }
    }
    static class MandelbrotGenerator
    {
        const int MaxIterations = 1000;
        const double SampleWidth = 3.2;
        const double SampleHeight = 2.5;
        const double OffsetX = -2.1;
        const double OffsetY = -1.25;

        const int ImageWidth = 900;
        const int ImageHeight = (int)(SampleHeight * ImageWidth / SampleWidth);

        static byte ComputeMandelbrotIndex(int row, int col)
        {
            double x = (col * SampleWidth) / ImageWidth + OffsetX;
            double y = (row * SampleHeight) / ImageHeight + OffsetY;

            double y0 = y;
            double x0 = x;

            for (int i = 0; i < MaxIterations; i++)
            {
                if (x * x + y * y >= 4)
                {
                    return (byte)((i % 255) + 1);
                }
                double xtemp = x * x - y * y + x0;
                y = 2 * x * y + y0;
                x = xtemp;
            }
            return 0;
        }
        public static Bitmap Generate()
        {
            var points = from row in Enumerable.Range(0, ImageHeight)
                         from col in Enumerable.Range(0, ImageWidth)
                         select new { row, col };

            var query = from point in points.AsParallel().AsOrdered()
                        select ComputeMandelbrotIndex(point.row, point.col);

            byte[] data = query.ToArray();
            unsafe
            {
                fixed (byte* ptr = data)
                {
                    IntPtr scan0 = new IntPtr(ptr);
                    return new Bitmap(ImageWidth, ImageHeight, ImageWidth,
                        PixelFormat.Format8bppIndexed, scan0);
                }
            }
        }
    }
}

Download the complete project

April 11th, 2011 — James

If you are using Visual Studio 2008, just open the project file and an entry to the assembly you need to refer to.

Locate the Item Group that has references. Take any one of the system related assembly reference and change the Include entry.

<Reference Include="Your.GAC.Assembly.Name" />
March 17th, 2011 — James
public string GetFileName(string path)
{
    FileInfo finfo = new FileInfo(path);
    string FileName = finfo.Name;
    string FileExtn  = finfo.Extension;
    return FileName.Replace(FileExtn, string.Empty);
    // Or just return finfo.Name.Replace(finfo.Extension, string.Empty);
}

Sample:
Input: c:\temp\sampletext.txt
Output: sampletext

namespace needed: System.IO

January 26th, 2011 — James

Here is the code sample to get the name of the directory from a full path. The actual directory do not have to exist to execute this code:

string fullPath = @"c:\rootFolder\secondLevel\Thirdlevel";
Console.WriteLine(new DirectoryInfo(fullPath).Name);

This should output just “Thirdlevel”.

But if the path has file name in it, then you need to strip that away.